diff --git a/arces/arcespipe.py b/arces/arcespipe.py index 0b35cf3..2065e63 100644 --- a/arces/arcespipe.py +++ b/arces/arcespipe.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use('Agg') @@ -81,7 +82,7 @@ ####### GLOBAL VARIABLES ##### force_pre_process = False -force_flat = False +force_flat = False force_P = False force_flat_extract = False force_thar_extract = False @@ -102,7 +103,7 @@ NSigma_Marsh = 30 NCosmic_Marsh = 10 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 200 max_extract_col = 1847 @@ -118,59 +119,59 @@ ############################# log = dirout+'night.log' -print "\n\n\tARCES APO3.5m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tARCES APO3.5m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') biases, quartzB, quartzR, science, thars, thar_dates, obnames, exptimes = arcesutils.FileClassify(dirin,log,binning) nightlog = open(log,'r') loglines = nightlog.readlines() -print "\tThese are all the images to proccess:" +print("\tThese are all the images to proccess:") for line in loglines: - print '\t'+line[:-1] -print '\n' + print('\t'+line[:-1]) +print('\n') if stst == 'last': - if os.access(dirout+'findstar.txt',os.F_OK): - fst = open(dirout+'findstar.txt','r') - stst = fst.readline() - fst.close() - else: - raise ValueError("There is not a previously defined standard star file!!! You have to enter one (i.e. -ofind pfs0001.fits).\n") + if os.access(dirout+'findstar.txt',os.F_OK): + fst = open(dirout+'findstar.txt','r') + stst = fst.readline() + fst.close() + else: + raise ValueError("There is not a previously defined standard star file!!! You have to enter one (i.e. -ofind pfs0001.fits).\n") else: - fst = open(dirout+'findstar.txt','w') - fst.write(stst+'\n') - fst.close() - -if ( (os.access(dirout+'FlatB.fits', os.F_OK) == False) or \ - (os.access(dirout+'FlatR.fits', os.F_OK) == False) or \ - (os.access(dirout+'MasterBias.fits', os.F_OK) == False) or \ - (os.access(dirout+'trace.pkl', os.F_OK) == False) or \ + fst = open(dirout+'findstar.txt','w') + fst.write(stst+'\n') + fst.close() + +if ( (os.access(dirout+'FlatB.fits', os.F_OK) == False) or \ + (os.access(dirout+'FlatR.fits', os.F_OK) == False) or \ + (os.access(dirout+'MasterBias.fits', os.F_OK) == False) or \ + (os.access(dirout+'trace.pkl', os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine Biases - print "\tGenerating Master calibration frames..." + print("\tGenerating Master calibration frames...") if len(biases)!=0: - MasterBias, RO_bias, GA_bias = arcesutils.MedianCombine(biases) - print "\t\t-> Masterbias: done!" + MasterBias, RO_bias, GA_bias = arcesutils.MedianCombine(biases) + print("\t\t-> Masterbias: done!") else: - MasterBias, RO_bias, GA_bias = np.zeros((2048,2048)),0,1 - print "\t\t\tWarning: 0 biases" + MasterBias, RO_bias, GA_bias = np.zeros((2048,2048)),0,1 + print("\t\t\tWarning: 0 biases") hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): - os.remove(dirout+'MasterBias.fits') + os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') # median combine list of ob flats FlatR, RO_flatR, GA_flatR = arcesutils.MedianCombine(quartzR, bias = MasterBias) FlatB, RO_flatB, GA_flatB = arcesutils.MedianCombine(quartzB, bias = MasterBias) - print "\t\t-> Masterflats: done!" + print("\t\t-> Masterflats: done!") # save this file for later reference hdu = pyfits.PrimaryHDU( FlatR ) @@ -181,11 +182,11 @@ if (os.access(dirout+'FlatB.fits',os.F_OK)): os.remove(dirout+'FlatB.fits') hdu.writeto(dirout+'FlatB.fits') - + # Find orders & traces - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all, nord = GLOBALutils.get_them( FlatB + FlatR, ext_aperture, trace_degree, maxords=-1,mode=1 ) - print "\t\t\t", nord, 'orders found ...' + print("\t\t\t", nord, 'orders found ...') trace_dict = {'c_all':c_all, 'nord':nord, 'GA_bias': GA_bias, 'RO_bias': RO_bias, \ 'GA_flatB': GA_flatB, 'RO_flatB': RO_flatB, 'GA_flatR': GA_flatR, 'RO_flatR': RO_flatR} @@ -193,7 +194,7 @@ else: - print '\tLoading Masterbias, Masterflat and traces' + print('\tLoading Masterbias, Masterflat and traces') trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) c_all = trace_dict['c_all'] nord = trace_dict['nord'] @@ -214,84 +215,84 @@ Pref_fits = dirout + 'P_ref.fits' force_Pref = False if ( os.access(Pref_fits,os.F_OK) == False ) or (force_Pref): - print "\n\tDetermining reference weights for optimal extraction..." - h = pyfits.open(dirin+stst)[0] - ron = h.header['RDNOISE'] - gain = h.header['GAIN'] - hth = pyfits.getheader(dirin+stst) - d = h.data - d = arcesutils.OverscanTrim(d) - d = arcesutils.bad_col_corr(d) - d -= MasterBias - c_alls = c_all.copy() - Centers = np.zeros((len(c_alls),d.shape[1])) - for i in range(nord): - Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) - bkg_obj_fits = dirout + 'BKG_' + 'Pref.fits' - if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): - bkg = GLOBALutils.get_scat(d,Centers,span=4) - if (os.access(bkg_obj_fits,os.F_OK)): - os.remove( bkg_obj_fits ) - hdu = pyfits.PrimaryHDU( bkg ) - hdu.writeto( bkg_obj_fits ) - else: - bkg = pyfits.getdata(bkg_obj_fits) - d -= bkg - if os.access(Pref_fits,os.F_OK) == False or force_P: - P_ref = np.zeros( d.shape ) - for i in range(nord): - P_marsh = GLOBALutils.PCoeff( d, c_alls[i,:], ext_aperture, ron, gain, NSigma_Marsh, S_Marsh, N_Marsh, Marsh_alg , min_extract_col, max_extract_col ) - P_ref += P_marsh - - if (os.access(Pref_fits,os.F_OK)): - os.remove( Pref_fits ) - hdu = pyfits.PrimaryHDU( P_ref ) - hdu.writeto( Pref_fits ) + print("\n\tDetermining reference weights for optimal extraction...") + h = pyfits.open(dirin+stst)[0] + ron = h.header['RDNOISE'] + gain = h.header['GAIN'] + hth = pyfits.getheader(dirin+stst) + d = h.data + d = arcesutils.OverscanTrim(d) + d = arcesutils.bad_col_corr(d) + d -= MasterBias + c_alls = c_all.copy() + Centers = np.zeros((len(c_alls),d.shape[1])) + for i in range(nord): + Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) + bkg_obj_fits = dirout + 'BKG_' + 'Pref.fits' + if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): + bkg = GLOBALutils.get_scat(d,Centers,span=4) + if (os.access(bkg_obj_fits,os.F_OK)): + os.remove( bkg_obj_fits ) + hdu = pyfits.PrimaryHDU( bkg ) + hdu.writeto( bkg_obj_fits ) + else: + bkg = pyfits.getdata(bkg_obj_fits) + d -= bkg + if os.access(Pref_fits,os.F_OK) == False or force_P: + P_ref = np.zeros( d.shape ) + for i in range(nord): + P_marsh = GLOBALutils.PCoeff( d, c_alls[i,:], ext_aperture, ron, gain, NSigma_Marsh, S_Marsh, N_Marsh, Marsh_alg , min_extract_col, max_extract_col ) + P_ref += P_marsh + + if (os.access(Pref_fits,os.F_OK)): + os.remove( Pref_fits ) + hdu = pyfits.PrimaryHDU( P_ref ) + hdu.writeto( Pref_fits ) else: - print "\tWeights for optimal extraction loaded..." - P_ref = pyfits.getdata(Pref_fits) + print("\tWeights for optimal extraction loaded...") + P_ref = pyfits.getdata(Pref_fits) # Extract Flat -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') Flat_spec_fits = dirout + 'Flat_spec.fits' P_fits = dirout + 'P_flat.fits' if ( os.access(Flat_spec_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo previous Flat extracted or extraction forced, extracting and saving..." - c_alls = c_all.copy() - Centers = np.zeros((len(c_alls),FlatB.shape[1])) - - if os.access(P_fits,os.F_OK) == False or force_P: - P = np.zeros( FlatB.shape ) - for i in range(nord): - P_marsh = GLOBALutils.PCoeff( FlatR+FlatB, c_alls[i,:], ext_aperture,\ - RO_flatR, GA_flatR, NSigma_Marsh*10, S_Marsh, 2, Marsh_alg,\ - min_extract_col, max_extract_col ) - P += P_marsh - if (os.access(P_fits,os.F_OK)): - os.remove( P_fits ) - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( P_fits ) - else: - P = pyfits.getdata(P_fits) - - flat_S = GLOBALutils.optimal_extraction(FlatB+FlatR,P,c_all,ext_aperture,RO_flatB,GA_flatB,\ - S_Marsh,10*NCosmic_Marsh,min_extract_col,max_extract_col,npools) - - for i in range(nord): - flat_S[i,1,:] = flat_S[i,1,:][::-1] - flat_S[i,2,:] = flat_S[i,2,:][::-1] - - if (os.access(Flat_spec_fits,os.F_OK)): - os.remove( Flat_spec_fits ) - hdu = pyfits.PrimaryHDU( flat_S ) - hdu.writeto( Flat_spec_fits ) -else: - print "\tExtracted flat found, loading..." - flat_S = pyfits.getdata( Flat_spec_fits ) + print("\t\tNo previous Flat extracted or extraction forced, extracting and saving...") + c_alls = c_all.copy() + Centers = np.zeros((len(c_alls),FlatB.shape[1])) + + if os.access(P_fits,os.F_OK) == False or force_P: + P = np.zeros( FlatB.shape ) + for i in range(nord): + P_marsh = GLOBALutils.PCoeff( FlatR+FlatB, c_alls[i,:], ext_aperture,\ + RO_flatR, GA_flatR, NSigma_Marsh*10, S_Marsh, 2, Marsh_alg,\ + min_extract_col, max_extract_col ) + P += P_marsh + if (os.access(P_fits,os.F_OK)): + os.remove( P_fits ) + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( P_fits ) + else: + P = pyfits.getdata(P_fits) + + flat_S = GLOBALutils.optimal_extraction(FlatB+FlatR,P,c_all,ext_aperture,RO_flatB,GA_flatB,\ + S_Marsh,10*NCosmic_Marsh,min_extract_col,max_extract_col,npools) + + for i in range(nord): + flat_S[i,1,:] = flat_S[i,1,:][::-1] + flat_S[i,2,:] = flat_S[i,2,:][::-1] + + if (os.access(Flat_spec_fits,os.F_OK)): + os.remove( Flat_spec_fits ) + hdu = pyfits.PrimaryHDU( flat_S ) + hdu.writeto( Flat_spec_fits ) +else: + print("\tExtracted flat found, loading...") + flat_S = pyfits.getdata( Flat_spec_fits ) thar_az,thar_al = [],[] thar_ra,thar_dec = [],[] -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in thars: hthar = pyfits.open( fsim )[0] @@ -310,23 +311,23 @@ thar_fits = dirout + 'ARCES_' + hthar.header['DATE-OBS'] + '.ThAr.spec.fits' if ( os.access(thar_fits,os.F_OK) == False ) or (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") thar_S = np.zeros( (nord,dthar.shape[1]) ) - thar_S = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) + thar_S = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) for i in range(nord): thar_S[i] = thar_S[i][::-1] - + # save as fits file if (os.access(thar_fits,os.F_OK)): os.remove( thar_fits ) hdu = pyfits.PrimaryHDU( thar_S ) hdu.writeto( thar_fits ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") thar_az, thar_al = np.array(thar_az), np.array(thar_al) # Compute wavelength calibration of ThAr -print '\n\tWavelength solution of ThAr calibration spectra:' +print('\n\tWavelength solution of ThAr calibration spectra:') sorted_thar_dates = np.argsort( thar_dates ) badind = [] for i in range(len(thars)): @@ -334,18 +335,18 @@ hthar = pyfits.open( thars[index] ) wavsol_pkl = dirout + 'ARCES_' + '_' + hthar[0].header['DATE-OBS']+'.wavsolpars.pkl' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on initial ThAr file", thars[index] - + print("\t\tWorking on initial ThAr file", thars[index]) + mjd, mjd0 = arcesutils.mjd_fromheader( hthar ) thar_fits = dirout + 'ARCES_' + hthar[0].header['DATE-OBS'] + '.ThAr.spec.fits' thar_S = pyfits.getdata( thar_fits ) - hd = pyfits.getheader(thar_fits) - thar_out = dirout + 'ARCES_' + hthar[0].header['DATE-OBS'] + '.ThAr.wav.fits' + hd = pyfits.getheader(thar_fits) + thar_out = dirout + 'ARCES_' + hthar[0].header['DATE-OBS'] + '.ThAr.wav.fits' - thar_data = np.zeros((2,n_useful,thar_S.shape[1])) + thar_data = np.zeros((2,n_useful,thar_S.shape[1])) lines_thar = thar_S.copy() - + All_Pixel_Centers = np.array([]) All_Wavelengths = np.array([]) All_Orders = np.array([]) @@ -353,52 +354,52 @@ All_Sigmas = np.array([]) All_Intensities = np.array([]) - force_corr = True - if os.access(dirout+'id_orders.pkl',os.F_OK) == False or force_corr: - maxes = 0 - or41 = 0 - for order in range(len(lines_thar)): - ccf_max, shift = GLOBALutils.cor_thar(lines_thar[order],span=10,filename=order_dir+'arces_order41.dat') - if ccf_max > maxes: - maxes = ccf_max - rough_shift = shift - or41 = order - print '\t\t\tThe real echelle order 41 is order',or41 - print '\t\t\tShift in pixels:',rough_shift - or0 = or41 - 41 - or10 = 10 + or0 - - if or0 >= 0: - orwa = 0 - else: - orwa = - or0 - or0 = 0 - if os.access(dirout+'id_orders.pkl',os.F_OK): - os.remove(dirout+'id_orders.pkl') - - pdict = {'or0':or0, 'orwa':orwa, 'or10':or10, 'rough_shift':rough_shift} - pickle.dump( pdict, open(dirout+'id_orders.pkl', 'w' ) ) - else: - pdict = pickle.load(open(dirout+'id_orders.pkl','r')) - or10 = pdict['or10'] - - - order = or10 - worder = 10 - medl = [] + force_corr = True + if os.access(dirout+'id_orders.pkl',os.F_OK) == False or force_corr: + maxes = 0 + or41 = 0 + for order in range(len(lines_thar)): + ccf_max, shift = GLOBALutils.cor_thar(lines_thar[order],span=10,filename=order_dir+'arces_order41.dat') + if ccf_max > maxes: + maxes = ccf_max + rough_shift = shift + or41 = order + print('\t\t\tThe real echelle order 41 is order',or41) + print('\t\t\tShift in pixels:',rough_shift) + or0 = or41 - 41 + or10 = 10 + or0 + + if or0 >= 0: + orwa = 0 + else: + orwa = - or0 + or0 = 0 + if os.access(dirout+'id_orders.pkl',os.F_OK): + os.remove(dirout+'id_orders.pkl') + + pdict = {'or0':or0, 'orwa':orwa, 'or10':or10, 'rough_shift':rough_shift} + pickle.dump( pdict, open(dirout+'id_orders.pkl', 'w' ) ) + else: + pdict = pickle.load(open(dirout+'id_orders.pkl','r')) + or10 = pdict['or10'] + + + order = or10 + worder = 10 + medl = [] while order <= or10 + n_useful: order_s = str(worder) if (worder < 10): order_s = '0'+str(worder) - + thar_order_orig = lines_thar[order,:] thar_order = thar_order_orig - scipy.signal.medfilt(thar_order_orig, 21) - + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ = GLOBALutils.Initial_Wav_Calibration(order_dir+'arces_order'+order_s+'.dat', thar_order, order, np.ones(len(thar_order)),rmsmax=1000, minlines=4,FixEnds=False,Dump_Argon=dumpargon,Dump_AllLines=True, Cheby=use_cheby,porder=3,rough_shift=rough_shift) - medl.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,.5*len(thar_order),len(thar_order))) + medl.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,.5*len(thar_order),len(thar_order))) - if (worder == 55): + if (worder == 55): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order)) else: @@ -410,32 +411,32 @@ All_Centroids = np.append( All_Centroids, centroids) All_Sigmas = np.append( All_Sigmas, sigmas) All_Intensities = np.append( All_Intensities, intensities ) - - order +=1 - worder +=1 + + order +=1 + worder +=1 p0 = np.zeros( npar_wsol ) - p0[0] = (55+52) * Global_ZP + p0[0] = (55+52) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers,All_Wavelengths,All_Orders,\ np.ones(All_Intensities.shape),p0,Cheby=use_cheby,\ maxrms=100, Inv=Inverse_m,minlines=minlines_glob,order0=52,\ - ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - equis = np.arange( thar_S.shape[1] ) - order = 0 - while order < n_useful: + equis = np.arange( thar_S.shape[1] ) + order = 0 + while order < n_useful: m = order + 52 + 10 chebs = GLOBALutils.Calculate_chebs(equis, m, order0=52, ntotal=n_useful,\ - npix=thar_S.shape[1],Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = (1./m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) + npix=thar_S.shape[1],Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = (1./m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) thar_data[0,order] = WavSol - thar_data[1,order] = lines_thar[order+or10,:] - order += 1 + thar_data[1,order] = lines_thar[order+or10,:] + order += 1 - if (os.access(thar_out,os.F_OK)): + if (os.access(thar_out,os.F_OK)): os.remove( thar_out ) - + hdu = pyfits.PrimaryHDU( thar_data ) hdu.writeto( thar_out ) @@ -444,23 +445,23 @@ pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl,'r')) - + if pdict['rms_ms']/np.sqrt(float(len(pdict['II']))) > 10: - badind.append(index) + badind.append(index) pditct2 = pickle.load(open(dirout+'id_orders.pkl','r')) or10 = pditct2['or10'] thars = np.array(thars) thar_dates = np.array(thar_dates) if len(badind)>0: - thars = np.delete(thars,badind) - thar_dates = np.delete(thar_dates,badind) - thar_ra = np.delete(thar_ra,badind) - thar_dec = np.delete(thar_dec,badind) + thars = np.delete(thars,badind) + thar_dates = np.delete(thar_dates,badind) + thar_ra = np.delete(thar_ra,badind) + thar_dec = np.delete(thar_dec,badind) -print '\n\tStarting science frame reductions' +print('\n\tStarting science frame reductions') ### start of science frame reductions ### new_list = [] @@ -513,16 +514,16 @@ h = pyfits.open(fsim) - print "\n\t-->\tWorking on image: ", fsim + print("\n\t-->\tWorking on image: ", fsim) # get mjd and mjd0 mjd,mjd0 = arcesutils.mjd_fromheader(h) - # get gain and readnoise of object + # get gain and readnoise of object ronoise = h[0].header['RDNOISE'] gain = h[0].header['GAIN'] - print "\t\tObject name:",obname + print("\t\tObject name:",obname) # Find lambda_bary/lambda_topo using baryc altitude = 2788. @@ -532,7 +533,7 @@ ra = float(ra.split(':')[0]) + float(ra.split(':')[1])/60. + float(ra.split(':')[2])/3600. ra = ra * 360. / 24. dec = h[0].header['DEC'] - dec = float(dec.split(':')[0]) + float(dec.split(':')[1])/60. + float(dec.split(':')[2])/3600. + dec = float(dec.split(':')[0]) + float(dec.split(':')[1])/60. + float(dec.split(':')[2])/3600. epoch = h[0].header['EQUINOX'] ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) @@ -540,7 +541,7 @@ ra = ra2 dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -551,14 +552,14 @@ res = jplephem.doppler_fraction(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print '\t\tBarycentric velocity:', bcvel_baryc + print('\t\tBarycentric velocity:', bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name='APO3.5' - gobs.lat=rad(latitude) # lat/long in decimal degrees + gobs = ephem.Observer() + gobs.name='APO3.5' + gobs.lat=rad(latitude) # lat/long in decimal degrees gobs.long=rad(longitude) DDATE = h[0].header['DATE-OBS'].split('T')[0] @@ -576,7 +577,7 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sorted_indices = np.argsort( np.abs( np.array(thar_dates) - mjd ) ) @@ -611,8 +612,8 @@ bkg = pyfits.getdata(bkg_obj_fits) data -= bkg - print '\t\tExtraction:' - #print '\t\tComputing weights...' + print('\t\tExtraction:') + #print '\t\tComputing weights...' if os.access(P_fits,os.F_OK) == False or force_P: P = GLOBALutils.obtain_P(data,c_alls,ext_aperture,ronoise,\ @@ -625,7 +626,7 @@ else: P = pyfits.getdata(P_fits) - print "\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") sci_Ss = GLOBALutils.simple_extraction(data,c_alls,ext_aperture,\ min_extract_col,max_extract_col,npools) @@ -650,7 +651,7 @@ hdu.writeto( sci_fits_simple ) else: - print '\t\t'+fsim, "has already been extracted, reading in product fits files..." + print('\t\t'+fsim, "has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) @@ -667,7 +668,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'].split('T')[1]) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -679,7 +680,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',2788.) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',h[0].header['AIRMASS']) - print '\t\tWavelength calibration:' + print('\t\tWavelength calibration:') # get ThAr closest in time and position if len(sorted_indices)>1: indice1 = sorted_indices[0] @@ -701,16 +702,16 @@ hthar = pyfits.open(thars[indice]) thar_fits_ob = dirout + 'ARCES_' + hthar[0].header['DATE-OBS'] +'.ThAr.spec.fits' pkl_wsol = dirout + 'ARCES__' + hthar[0].header['DATE-OBS'] +'.wavsolpars.pkl' - print "\t\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + print("\t\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) - #print '\t\tMaking final output...' + equis = np.arange( data.shape[1] ) + #print '\t\tMaking final output...' for order in range(n_useful): m = order + 10 + 52 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1], order0=52, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) + WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) spec[0,order,:] = GLOBALutils.ToVacuum(WavSol) spec[1,order,:] = sci_S[order+or10,1, :] spec[2,order,:] = sci_S[order+or10,2, :] @@ -723,7 +724,7 @@ spec[3,order,nJ2] = 1. IJJ = np.where(spec[3,order]!=0)[0] if len(IJJ)>0: - cont_coef = GLOBALutils.get_cont_single(spec[0,order],spec[3,order],spec[4,order],ll=1.5,lu=5,nc=3) + cont_coef = GLOBALutils.get_cont_single(spec[0,order],spec[3,order],spec[4,order],ll=1.5,lu=5,nc=3) ratio = np.polyval(cont_coef, spec[0,order,:]) else: ratio = np.ones(len(spec[3,order])) @@ -743,7 +744,7 @@ NN = np.average(dlambda_dx) dlambda_dx /= NN - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) else: @@ -760,29 +761,29 @@ JustExtract = False if (not JustExtract): if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') # spectral analysis query_success = False query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + 'ARCES_' + h[0].header['DATE-OBS'] + '.' + obname +'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec,model_path=models_path,npools=npools) line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) f = open(pars_file,'w') f.write(line) f.close() else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -800,10 +801,10 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") # Read in mask # make mask larger accounting for factor ~3 lower res in Arces w/r to HARPS ml, mh, weight = np.loadtxt(mask,unpack=True) @@ -826,9 +827,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): # first rough correlation to find the minimum @@ -836,7 +837,7 @@ GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=300,vel_step=3,\ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - # Normalize the continuum of the CCF robustly with R + # Normalize the continuum of the CCF robustly with R yy = scipy.signal.medfilt(xc_av,11) I = np.where(np.isnan(yy))[0] if len(I)==0: @@ -844,15 +845,15 @@ tck1 = scipy.interpolate.splrep(vels,pred,k=1) xc_av_orig = xc_av.copy() xc_av /= pred - vel0_xc = vels[ np.argmin( xc_av ) ] + vel0_xc = vels[ np.argmin( xc_av ) ] rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.3,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.3,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) @@ -891,11 +892,11 @@ if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -908,13 +909,13 @@ if not problem: xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) @@ -933,24 +934,24 @@ C = 0.00181 elif sp_type == 'K5': A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 else: A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 BSerr = D / float(np.round(SNR_5130)) + C RVerr2 = 0.5 - RV = np.around(p1gau_m[1],3) - BS = np.around(SP,3) + RV = np.around(p1gau_m[1],3) + BS = np.around(SP,3) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -978,11 +979,11 @@ line_out = "%-15s %18.8f %8.3f %5.3f %5.3f %5.3f arces ceres 40000 %6d %4.1f %4.1f %5.1f %3.1f %3.1f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) + TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/arces/arcesutils.py b/arces/arcesutils.py index d550a08..3c0438a 100644 --- a/arces/arcesutils.py +++ b/arces/arcesutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function from astropy.io import fits as pyfits import scipy import glob @@ -12,7 +13,7 @@ def FileClassify(dir, log,binning): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -31,46 +32,46 @@ def FileClassify(dir, log,binning): bad_files = [] if os.access(dir+'bad_files.txt',os.F_OK): - bf = open(dir+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(dir+line[:-1]) - bf.close() - + bf = open(dir+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(dir+line[:-1]) + bf.close() + all_files = glob.glob(dir+"/*fits") for archivo in all_files: - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - h = pyfits.open(archivo) - if dump == False and h[0].header['INSTRUME'] == 'echelle': - if h[0].header['IMAGETYP'] == 'object': - science.append(archivo) - obname = h[0].header['OBJNAME'] - ra = h[0].header['RA'] - delta = h[0].header['DEC'] - airmass = h[0].header['AIRMASS'] - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'][:10] - hour = h[0].header['DATE-OBS'][11:] - obnames.append( obname ) - exptimes.append( texp ) - line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) - f.write(line) - elif h[0].header['IMAGETYP'] == 'zero': - biases.append(archivo) - elif h[0].header['IMAGETYP'] == 'flat': - if h[0].header['FILTER'] == 'Blue': - quartzB.append(archivo) - elif h[0].header['FILTER'] == 'Open': - quartzR.append(archivo) - elif h[0].header['IMAGETYP'] == 'comp': - thars.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - thar_dates.append( mjd ) - + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + h = pyfits.open(archivo) + if dump == False and h[0].header['INSTRUME'] == 'echelle': + if h[0].header['IMAGETYP'] == 'object': + science.append(archivo) + obname = h[0].header['OBJNAME'] + ra = h[0].header['RA'] + delta = h[0].header['DEC'] + airmass = h[0].header['AIRMASS'] + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'][:10] + hour = h[0].header['DATE-OBS'][11:] + obnames.append( obname ) + exptimes.append( texp ) + line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) + f.write(line) + elif h[0].header['IMAGETYP'] == 'zero': + biases.append(archivo) + elif h[0].header['IMAGETYP'] == 'flat': + if h[0].header['FILTER'] == 'Blue': + quartzB.append(archivo) + elif h[0].header['FILTER'] == 'Open': + quartzR.append(archivo) + elif h[0].header['IMAGETYP'] == 'comp': + thars.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + thar_dates.append( mjd ) + f.close() return biases, quartzB, quartzR, science, thars, thar_dates, obnames, exptimes @@ -79,7 +80,7 @@ def mjd_fromheader(h): """ return modified Julian date from header """ - + datetu = h[0].header['DATE-OBS'][:10] ut = h[0].header['DATE-OBS'][11:] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:])) @@ -93,31 +94,31 @@ def mjd_fromheader(h): return mjd, mjd0 def bad_col_corr(dat): - mat = np.array([[80 ,81 ,199 ,2048],\ - [240 ,241 ,530 ,2048],\ - [469 ,470 ,1555,1666],\ - [547 ,548 ,1690,1703],\ - [632 ,634 ,1985,1962],\ - [766 ,767 ,782 ,1980],\ - [832 ,833 ,1850,1906],\ - [960 ,961, 1590,1871],\ - [1263,1264,1772,1816],\ - [1334,1335,1454,2048],\ - [1361,1362,1885,1924],\ - [1395,1396,1906,1955],\ - [1460,1461,488 ,2048],\ - [1477,1478,758 ,2048],\ - [1510,1511,771 ,2048],\ - [1581,1582,1398,1763],\ - [1629,1630,420 ,2048],\ - [1661,1662,643 ,1980]]) - for bd in mat: - new = .5*( dat[bd[2]:bd[3], bd[0]-1] + dat[bd[2]:bd[3],bd[1]] ) - bci = bd[0] - while bci < bd[1]: - dat[ bd[2]:bd[3], bci ] = new - bci += 1 - return dat + mat = np.array([[80 ,81 ,199 ,2048],\ + [240 ,241 ,530 ,2048],\ + [469 ,470 ,1555,1666],\ + [547 ,548 ,1690,1703],\ + [632 ,634 ,1985,1962],\ + [766 ,767 ,782 ,1980],\ + [832 ,833 ,1850,1906],\ + [960 ,961, 1590,1871],\ + [1263,1264,1772,1816],\ + [1334,1335,1454,2048],\ + [1361,1362,1885,1924],\ + [1395,1396,1906,1955],\ + [1460,1461,488 ,2048],\ + [1477,1478,758 ,2048],\ + [1510,1511,771 ,2048],\ + [1581,1582,1398,1763],\ + [1629,1630,420 ,2048],\ + [1661,1662,643 ,1980]]) + for bd in mat: + new = .5*( dat[bd[2]:bd[3], bd[0]-1] + dat[bd[2]:bd[3],bd[1]] ) + bci = bd[0] + while bci < bd[1]: + dat[ bd[2]:bd[3], bci ] = new + bci += 1 + return dat def MedianCombine(ImgList, bias = 0.): """ @@ -125,14 +126,14 @@ def MedianCombine(ImgList, bias = 0.): """ n = len(ImgList) if n == 0: - print "\t\tWarning: 0 biases" - return 0, 0, 0 + print("\t\tWarning: 0 biases") + return 0, 0, 0 h = pyfits.open(ImgList[0])[0] d = h.data d = OverscanTrim(d) d = bad_col_corr(d) d -= bias - + factor = 1.25 if (n < 3): factor = 1 @@ -140,15 +141,15 @@ def MedianCombine(ImgList, bias = 0.): ronoise = factor * h.header['RDNOISE'] / np.sqrt(n) gain = h.header['GAIN'] - + if (n == 1): return d, ronoise, gain else: for i in range(n-1): h = pyfits.open(ImgList[i+1])[0] - #print ImgList[i+1], h.data.shape - d2 = OverscanTrim(h.data) - d2 = bad_col_corr(d2) + #print ImgList[i+1], h.data.shape + d2 = OverscanTrim(h.data) + d2 = bad_col_corr(d2) d = np.dstack( (d ,d2 - bias )) return np.median(d,axis=2), ronoise, gain @@ -161,7 +162,7 @@ def OverscanTrim(d): newdata = d[20:,21:-59] overscan = np.zeros(newdata.shape) for i in range(overscan.shape[1]): - overscan[:,i] = ov + overscan[:,i] = ov newdata = newdata - overscan return newdata diff --git a/cafe/cafepipe.py b/cafe/cafepipe.py index 415d72e..1ad66fc 100644 --- a/cafe/cafepipe.py +++ b/cafe/cafepipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * base = '../' @@ -82,7 +83,7 @@ force_pre_process = False force_flat_extract = False force_sci_extract = False -force_thar_extract = False +force_thar_extract = False force_tharxc = False force_thar_wavcal = False force_spectral_file_build = True @@ -100,13 +101,13 @@ NSigma_Marsh = 5 NCosmic_Marsh = 5 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 200 max_extract_col = 1848 if '2013' in dirin: - min_extract_col = 100 - max_extract_col = 1800 + min_extract_col = 100 + max_extract_col = 1800 ncoef_x = 3 ncoef_m = 8 @@ -122,43 +123,43 @@ # file containing the log log = dirout+'night.log' -print "\n\n\tCAFE CAHA2.2m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tCAFE CAHA2.2m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') biases, flats, ThAr_ref, sim_sci, ThAr_ref_dates, obnames, exptimes = cafeutils.FileClassify(dirin,log) IS = np.argsort(ThAr_ref_dates) ThAr_ref_dates = ThAr_ref_dates[IS] ThAr_ref = ThAr_ref[IS] -print '\tThese are all the images to proccess:' +print('\tThese are all the images to proccess:') f = open(log) flines = f.readlines() for line in flines: - print '\t'+line[:-1] -print '\n' + print('\t'+line[:-1]) +print('\n') if ( (os.access(dirout+'Flat.fits',os.F_OK) == False) or \ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine Biases - print "\tGenerating Master calibration frames..." + print("\tGenerating Master calibration frames...") MasterBias, RO_bias, GA_bias = cafeutils.MedianCombine(biases) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") # median combine list of flats Flat, RO_fl, GA_fl = cafeutils.MedianCombine(flats, zero=dirout+'MasterBias.fits') @@ -166,15 +167,15 @@ if (os.access(dirout+'Flat.fits',os.F_OK)): os.remove(dirout+'Flat.fits') hdu.writeto(dirout+'Flat.fits') - print "\t\t-> Masterflats: done!" + print("\t\t-> Masterflats: done!") - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all,nord = GLOBALutils.get_them(Flat, ext_aperture, trace_degree, maxords=-1, mode=1) c_all, nord = GLOBALutils.good_orders(c_all,nord,Flat.shape[0],Flat.shape[1],ext_aperture) if nord >= 90: c_all = c_all[:90] nord=90 - print '\t\t'+ str(nord)+' orders found.' + print('\t\t'+ str(nord)+' orders found.') # pickle traces trace_dict = {'c_all':c_all, @@ -198,7 +199,7 @@ h = pyfits.open(dirout+'MasterBias.fits') MasterBias = h[0].data -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') P_fits = dirout + 'P.fits' S_flat_fits = dirout +'flat.fits' @@ -207,7 +208,7 @@ if ( os.access(P_fits,os.F_OK) == False ) or \ ( os.access(S_flat_fits,os.F_OK) == False ) or \ (force_flat_extract): - print "\t\tNo extracted flat object spectra found or extraction forced, extracting and saving..." + print("\t\tNo extracted flat object spectra found or extraction forced, extracting and saving...") Centers = np.zeros((len(c_all),Flat.shape[1])) for i in range(nord): @@ -221,11 +222,11 @@ #print gfd bacfile = dirout + 'BAC_FLAT.fits' if (os.access(bacfile,os.F_OK)): - os.remove( bacfile ) + os.remove( bacfile ) hdbac = pyfits.PrimaryHDU( bac ) hdbac.writeto(bacfile) - print "\t\tWill extract",nord,"orders for object fibre..." + print("\t\tWill extract",nord,"orders for object fibre...") P = GLOBALutils.obtain_P(fl,c_all,ext_aperture,RO_fl,\ GA_fl,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_col,\ @@ -239,8 +240,8 @@ S_flat = GLOBALutils.optimal_extraction(fl,P,c_all,ext_aperture,RO_fl,GA_fl,\ S_Marsh,10*NCosmic_Marsh,min_extract_col,max_extract_col,npools) for i in range(nord): - S_flat[i,1] = S_flat[i,1][::-1] - S_flat[i,2] = S_flat[i,2][::-1] + S_flat[i,1] = S_flat[i,1][::-1] + S_flat[i,2] = S_flat[i,2][::-1] if (os.access(S_flat_fits,os.F_OK)): os.remove( S_flat_fits ) @@ -248,7 +249,7 @@ hdu.writeto( S_flat_fits ) else: - print "\t\tExtracted flat object spectra found, loading..." + print("\t\tExtracted flat object spectra found, loading...") P = pyfits.getdata( P_fits ) S_flat = pyfits.getdata( S_flat_fits ) @@ -258,11 +259,11 @@ new_list = [] new_times = [] for fsim in sim_sci: - h = pyfits.open(fsim) - if object2do in h[0].header['OBJECT'] or object2do == 'all': - new_list.append(fsim) - mjd, mjd0 = cafeutils.mjd_fromheader2( h ) - new_times.append(mjd) + h = pyfits.open(fsim) + if object2do in h[0].header['OBJECT'] or object2do == 'all': + new_list.append(fsim) + mjd, mjd0 = cafeutils.mjd_fromheader2( h ) + new_times.append(mjd) new_list = np.array(new_list) new_times = np.array(new_times) @@ -284,7 +285,7 @@ ThAr_ref_dates = ThAr_ref_dates[I1:I2+1] ThAr_ref = ThAr_ref[I1:I2+1] -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in ThAr_ref: @@ -292,35 +293,35 @@ thar_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' if ( os.access(thar_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): - hthar = pyfits.open( fsim ) - dthar = cafeutils.OverscanTrim( hthar[0].data ) - MasterBias + hthar = pyfits.open( fsim ) + dthar = cafeutils.OverscanTrim( hthar[0].data ) - MasterBias Centers = np.zeros((len(c_all),dthar.shape[1])) for i in range(nord): Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) bac = GLOBALutils.get_scat(dthar,Centers,span=7) - sdthar = dthar - bac - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + sdthar = dthar - bac + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") thar_S = np.zeros( (nord,3,dthar.shape[1]) ) - thar_Ss = np.zeros( (nord,dthar.shape[1]) ) + thar_Ss = np.zeros( (nord,dthar.shape[1]) ) - tR,tG = hthar[0].header['CCDRON'],hthar[0].header['CCDGAIN'] - #print tR,tG - thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,\ + tR,tG = hthar[0].header['CCDRON'],hthar[0].header['CCDGAIN'] + #print tR,tG + thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,\ min_extract_col,max_extract_col,npools) for i in range(nord): thar_Ss[i] = thar_Ss[i][::-1] # save as fits file if (os.access(thar_fits,os.F_OK)): os.remove( thar_fits ) - if (os.access(thar_simple_fits,os.F_OK)): + if (os.access(thar_simple_fits,os.F_OK)): os.remove( thar_simple_fits ) - + hdu = pyfits.PrimaryHDU( thar_S ) hdu.writeto( thar_fits ) hdu = pyfits.PrimaryHDU( thar_Ss ) hdu.writeto( thar_simple_fits ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") or50 = 50 or502 = 50 @@ -330,15 +331,15 @@ thar_Ss = pyfits.getdata(thar_fits) for i in range(40,60,1): - ccf_max, shift = GLOBALutils.cor_thar(thar_Ss[i],span=40,filename=order_dir+'order_103.iwdat') - if ccf_max > maxxc: - maxxc = ccf_max - rough_shift = shift - or50 = i + ccf_max, shift = GLOBALutils.cor_thar(thar_Ss[i],span=40,filename=order_dir+'order_103.iwdat') + if ccf_max > maxxc: + maxxc = ccf_max + rough_shift = shift + or50 = i difo = 103 - or50 #show() #print gfds -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") for i in range(len(ThAr_ref_dates)): wavsol_pkl = dirout + ThAr_ref[i].split('/')[-1][:-4]+'wavsolpars.pkl' @@ -346,41 +347,41 @@ gain,ron = float(hd['CCDGAIN']),float(hd['CCDRON']) #force_thar_wavcal = True if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on initial ThAr file", ThAr_ref[i] + print("\t\tWorking on initial ThAr file", ThAr_ref[i]) hthar = pyfits.open( ThAr_ref[i] ) mjd, mjd0 = cafeutils.mjd_fromheader2( hthar ) thar_fits = dirout + ThAr_ref[i].split('/')[-1][:-4]+'spec.simple.fits.S' thar_S = pyfits.getdata( thar_fits ) lines_thar = thar_S.copy() #iv_thar_ob_B = thar_S_ob_B[:,2,:] - c_p2w = np.zeros((n_useful,4)) - wavs,ords = [],[] - All_Pixel_Centers = np.array([]) + c_p2w = np.zeros((n_useful,4)) + wavs,ords = [],[] + All_Pixel_Centers = np.array([]) All_Wavelengths = np.array([]) All_Orders = np.array([]) All_Centroids = np.array([]) All_Sigmas = np.array([]) All_Intensities = np.array([]) - All_residuals = np.array([]) - torder = 0 - for order in range(ro0,ro0+n_useful+1,1): - #print order + All_residuals = np.array([]) + torder = 0 + for order in range(ro0,ro0+n_useful+1,1): + #print order order_s = str(order) if (order < 100): order_s = '0' + str(order) thar_order_orig = lines_thar[order - difo] #IV = iv_thar_ob_R[order,:] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. wei = np.sqrt( IV ) thar_order = thar_order_orig #- bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+'.iwdat', thar_order, order, np.ones(len(thar_order)), rmsmax=300, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby,porder=3,rough_shift=rough_shift) - if (order == int(.5*n_useful)+ro0): - + if (order == int(.5*n_useful)+ro0): + if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) else: @@ -392,24 +393,24 @@ All_Centroids = np.append( All_Centroids, centroids) All_Sigmas = np.append( All_Sigmas, sigmas) All_Intensities = np.append( All_Intensities, intensities ) - All_residuals = np.append( All_residuals, residuals ) - wavs.append(GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) )) - ords.append(torder) - torder += 1 - - p0 = np.zeros( npar_wsol ) - p0[0] = (int(.5*n_useful)+ro0) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ - All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ - order0=ro0, ntotal=n_useful, maxrms=200, Inv=Inverse_m, minlines=200, \ - npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + All_residuals = np.append( All_residuals, residuals ) + wavs.append(GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) )) + ords.append(torder) + torder += 1 + + p0 = np.zeros( npar_wsol ) + p0[0] = (int(.5*n_useful)+ro0) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ + All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ + order0=ro0, ntotal=n_useful, maxrms=200, Inv=Inverse_m, minlines=200, \ + npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths, 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers, 'All_Sigmas':All_Sigmas} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl,'r')) @@ -418,7 +419,7 @@ p_ref = dct['p1'] mjds_thar,shifts = [],[] -print '\n\tDetermination of instrumental drift during the night...' +print('\n\tDetermination of instrumental drift during the night...') temps = [] for i in range(len(ThAr_ref_dates)): wavsol_pkl = dirout + ThAr_ref[i].split('/')[-1][:-4]+'wavsolpars.pkl' @@ -429,40 +430,40 @@ mjds_thar.append(mjd) dct = pickle.load(open(wavsol_pkl,'r')) p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct['All_Pixel_Centers'], dct['All_Wavelengths'], dct['All_Orders'],\ - np.ones(len(dct['All_Orders'])), p_ref, order0=ro0, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=200, Inv=Inverse_m, minlines=500, nx=ncoef_x,nm=ncoef_m) + GLOBALutils.Global_Wav_Solution_vel_shift(dct['All_Pixel_Centers'], dct['All_Wavelengths'], dct['All_Orders'],\ + np.ones(len(dct['All_Orders'])), p_ref, order0=ro0, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=200, Inv=Inverse_m, minlines=500, nx=ncoef_x,nm=ncoef_m) shifts.append(p_shift[0]) mjds_thar,shifts,temps = np.array(mjds_thar),np.array(shifts),np.array(temps)-temps[0] shv = (1e-6*shifts)*299792458.0 if len(mjds_thar) > 1: - tck_v = scipy.interpolate.splrep(mjds_thar,shv,k=1) - tck_shift = scipy.interpolate.splrep(mjds_thar,shifts,k=1) - temp = np.arange(mjds_thar[0],mjds_thar[-1],0.0001) - - mjdsh_thar = (mjds_thar - int(temp.min())) * 24. - thour = (temp - int(temp.min())) * 24. - thours = (new_times - int(temp.min())) * 24. - pp = PdfPages(dirout + 'proc/ThAr_shifts.pdf') - plot(mjdsh_thar,shv,'ro') - plot(thour,scipy.interpolate.splev(temp,tck_v),'b') - for i in range(len(thours)): - axvline(x=thours[i],color ='g') - xlabel('UT [hours]') - ylabel('ThAr vel drift [m/s]') - savefig(dirout + 'proc/ThAr_shifts.pdf') - pp.savefig() - clf() - plot(shv,temps,'ro') - xlabel('ThAr velocity drift [m/s]') - ylabel('TEMP') - pp.savefig() - pp.close() - clf() - - thout = np.vstack((mjdsh_thar,shv)).T - np.savetxt(dirout + 'proc/ThAr_shifts.dat',thout) + tck_v = scipy.interpolate.splrep(mjds_thar,shv,k=1) + tck_shift = scipy.interpolate.splrep(mjds_thar,shifts,k=1) + temp = np.arange(mjds_thar[0],mjds_thar[-1],0.0001) + + mjdsh_thar = (mjds_thar - int(temp.min())) * 24. + thour = (temp - int(temp.min())) * 24. + thours = (new_times - int(temp.min())) * 24. + pp = PdfPages(dirout + 'proc/ThAr_shifts.pdf') + plot(mjdsh_thar,shv,'ro') + plot(thour,scipy.interpolate.splev(temp,tck_v),'b') + for i in range(len(thours)): + axvline(x=thours[i],color ='g') + xlabel('UT [hours]') + ylabel('ThAr vel drift [m/s]') + savefig(dirout + 'proc/ThAr_shifts.pdf') + pp.savefig() + clf() + plot(shv,temps,'ro') + xlabel('ThAr velocity drift [m/s]') + ylabel('TEMP') + pp.savefig() + pp.close() + clf() + + thout = np.vstack((mjdsh_thar,shv)).T + np.savetxt(dirout + 'proc/ThAr_shifts.dat',thout) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -489,19 +490,19 @@ know_moon = False if fsim.split('/')[-1] in spec_moon: - I = np.where(fsim.split('/')[-1] == spec_moon)[0] - know_moon = True - here_moon = use_moon[I] + I = np.where(fsim.split('/')[-1] == spec_moon)[0] + know_moon = True + here_moon = use_moon[I] - print '\n' - print "\t--> Working on image: ", fsim + print('\n') + print("\t--> Working on image: ", fsim) h = pyfits.open(fsim) mjd,mjd0 = cafeutils.mjd_fromheader2(h) ronoise, gain = float(h[0].header['CCDRON']),float(h[0].header['CCDGAIN']) # Object name obname = h[0].header['OBJECT'] - print "\t\tObject name:",obname + print("\t\tObject name:",obname) # Open file, trim, overscan subtract and MasterBias subtract data = cafeutils.b_col(h[0].data) @@ -517,7 +518,7 @@ Centers[i,:]=scipy.polyval(c_new[i,:],np.arange(len(Centers[i,:]))) bac = GLOBALutils.get_scat(data,Centers,span=7) if (os.access(bacfile,os.F_OK)): - os.remove( bacfile ) + os.remove( bacfile ) hdbac = pyfits.PrimaryHDU( bac ) hdbac.writeto(bacfile) else: @@ -533,10 +534,10 @@ ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 + ra = ra2 + dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -546,14 +547,14 @@ res = jplephem.doppler_fraction(float(ra/15.0), float(dec), long(mjd), float(mjd%1), 1, 0.0) lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) - gobs = ephem.Observer() + gobs = ephem.Observer() gobs.name = h[0].header['TELESCOP'] - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) gobs.date = h[0].header['DATE'][:10] + ' ' + h[0].header['DATE'][11:] mephem = ephem.Moon() mephem.compute(gobs) @@ -564,32 +565,32 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) moon_alts.update({fsim:mephem.alt}) moon_ills.update({fsim:lunation}) - print '\t\tExtraction:' + print('\t\tExtraction:') sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' sci_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or \ ( force_sci_extract ): - print "\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") sci_S = np.zeros( (nord,3,data.shape[1]) ) sci_Ss = np.zeros( (nord,data.shape[1]) ) - - order = ro0 - sci_Ss = GLOBALutils.simple_extraction(data,c_new,ext_aperture,\ + order = ro0 + + sci_Ss = GLOBALutils.simple_extraction(data,c_new,ext_aperture,\ min_extract_col,max_extract_col,npools) - sci_S = GLOBALutils.optimal_extraction(data,P_new,c_new,ext_aperture,\ + sci_S = GLOBALutils.optimal_extraction(data,P_new,c_new,ext_aperture,\ ronoise,gain,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - for i in range(nord): - sci_S[i,1,:] = sci_S[i,1,:][::-1] - sci_S[i,2,:] = sci_S[i,2,:][::-1] - sci_Ss[i,:] = sci_Ss[i][::-1] - + for i in range(nord): + sci_S[i,1,:] = sci_S[i,1,:][::-1] + sci_S[i,2,:] = sci_S[i,2,:][::-1] + sci_Ss[i,:] = sci_Ss[i][::-1] + if (os.access(sci_fits,os.F_OK)): os.remove( sci_fits ) if (os.access(sci_fits_simple,os.F_OK)): @@ -599,10 +600,10 @@ hdu.writeto( sci_fits ) hdu = pyfits.PrimaryHDU( sci_Ss ) hdu.writeto( sci_fits_simple ) - + else: - print '\t\t\t '+fsim+" has already been extracted, reading in product fits files..." + print('\t\t\t '+fsim+" has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) @@ -616,7 +617,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE'][11:]) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -627,43 +628,43 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',h[0].header['HIERARCH CAHA TEL GEOLON']) hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',h[0].header['HIERARCH CAHA TEL GEOELEV']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS START',h[0].header['AIRMASS']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON VEL',refvel) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON VEL',refvel) - print '\t\tWavelength calibration:' - print "\t\t\tInstrumental drift:",(1e-6*p_shift)*299792458.0 + print('\t\tWavelength calibration:') + print("\t\t\tInstrumental drift:",(1e-6*p_shift)*299792458.0) # Apply new wavelength solution including barycentric correction equis = np.arange( data.shape[1] ) - if len(mjds_thar) > 1: - p_shift = scipy.interpolate.splev(mjd,tck_shift) - else: - p_shift = 0. - order = ro0 - ind = 0 + if len(mjds_thar) > 1: + p_shift = scipy.interpolate.splev(mjd,tck_shift) + else: + p_shift = 0. + order = ro0 + ind = 0 while order < ro0 + n_useful: - oss = order - difo + oss = order - difo m = order - chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1], order0=ro0, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = GLOBALutils.ToVacuum( lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/float(m)) * \ - GLOBALutils.Joint_Polynomial_Cheby(p_ref,chebs,ncoef_x,ncoef_m) ) + chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1], order0=ro0, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = GLOBALutils.ToVacuum( lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/float(m)) * \ + GLOBALutils.Joint_Polynomial_Cheby(p_ref,chebs,ncoef_x,ncoef_m) ) spec[0,ind,:] = WavSol spec[1,ind,:] = sci_S[oss,1] spec[2,ind,:] = sci_S[oss,2] fn = S_flat[oss,1,:] - L = np.where( fn == 0 )[0] - spec[3,ind,:] = spec[1,ind,:] / S_flat[oss,1,:] + L = np.where( fn == 0 )[0] + spec[3,ind,:] = spec[1,ind,:] / S_flat[oss,1,:] spec[4,ind,:] = spec[2,ind] * ( S_flat_n[oss,1,:] ** 2 ) - spec[3,ind,L] = 0. - spec[4,ind,L] = 0. - ind+=1 - order+=1 - ccoefs = GLOBALutils.get_cont(spec[0,:,:],spec[3,:,:]) - order = ro0 - ind = 0 + spec[3,ind,L] = 0. + spec[4,ind,L] = 0. + ind+=1 + order+=1 + ccoefs = GLOBALutils.get_cont(spec[0,:,:],spec[3,:,:]) + order = ro0 + ind = 0 while order < ro0 + n_useful: - oss = order - difo - L = np.where( spec[1,ind] != 0 ) - spec[5,ind,:][L] = spec[3,ind][L] / np.polyval(ccoefs[ind],spec[0,ind][L]) + oss = order - difo + L = np.where( spec[1,ind] != 0 ) + spec[5,ind,:][L] = spec[3,ind][L] / np.polyval(ccoefs[ind],spec[0,ind][L]) ratio = np.polyval(ccoefs[ind],spec[0,ind][L]) * Snorms[oss] spec[6,ind,:][L] = spec[4,ind][L] * (ratio ** 2 ) spec[7,ind,:][L] = ratio @@ -672,22 +673,22 @@ dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) dlambda_dx /= NN - LL = np.where(spec[5,ind] > 1 + 10. / scipy.signal.medfilt(spec[8,ind],21))[0] - spec[5,ind,LL] = 1. + LL = np.where(spec[5,ind] > 1 + 10. / scipy.signal.medfilt(spec[8,ind],21))[0] + spec[5,ind,LL] = 1. - spec[9,ind][L] = spec[5,ind][L] * (dlambda_dx[L] ** 1) + spec[9,ind][L] = spec[5,ind][L] * (dlambda_dx[L] ** 1) spec[10,ind][L] = spec[6,ind][L] / (dlambda_dx[L] ** 2) - ind +=1 - order +=1 + ind +=1 + order +=1 - if os.access(dirout + fout, os.F_OK): - os.remove(dirout + fout) - hdu.writeto(dirout + fout) + if os.access(dirout + fout, os.F_OK): + os.remove(dirout + fout) + hdu.writeto(dirout + fout) if (not JustExtract): - if DoClass: - print '\t\tSpectral Analysis:' + if DoClass: + print('\t\tSpectral Analysis:') # spectral analysis # First, query SIMBAD with the object name query_success = False @@ -695,14 +696,14 @@ # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./60000.**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -713,37 +714,37 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 - - else: - T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) - T_eff_epoch = T_eff - logg_epoch = logg - Z_epoch = Z - vsini_epoch = vsini - vel0_epoch = vel0 - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - - print "\t\tRadial Velocity analysis:" + else: + T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + + T_eff_epoch = T_eff + logg_epoch = logg + Z_epoch = Z + vsini_epoch = vsini + vel0_epoch = vel0 + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) + + print("\t\tRadial Velocity analysis:") # assign mask - sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') + print("\t\t\tWill use",sp_type,"mask for CCF.") # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= (av_m - ml_v) @@ -752,21 +753,21 @@ #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' - disp = GLOBALutils.get_disp(obname, reffile=reffile) - if disp == 0: + disp = GLOBALutils.get_disp(obname, reffile=reffile) + if disp == 0: known_sigma = False if vsini != -999 and vsini != 0.: - disp = vsini - else: - disp = 3. + disp = vsini + else: + disp = 3. else: known_sigma = True mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): @@ -776,20 +777,20 @@ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - # Normalize the continuum of the CCF robustly with R + # Normalize the continuum of the CCF robustly with R yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) xc_av_orig = xc_av.copy() xc_av /= pred vel0_xc = vels[ np.argmin( xc_av ) ] - rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ - xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ + xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels if disp > 30: - disp = 30. + disp = 30. vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ @@ -799,94 +800,94 @@ xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred - - if sp_type == 'M5': - moon_sig = 2.5 - elif sp_type == 'K5': - moon_sig = 3.3 - else: - moon_sig = 4.5 + + if sp_type == 'M5': + moon_sig = 2.5 + elif sp_type == 'K5': + moon_sig = 3.3 + else: + moon_sig = 4.5 p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) - moonmatters = False - if (know_moon and here_moon): + moonmatters = False + if (know_moon and here_moon): moonmatters = True ismoon = True confused = False p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) moon_flag = 1 - else: + else: confused = False ismoon = False p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 moon_flag = 0 - bspan = GLOBALutils.calc_bss(vels,xc_av) - SP = bspan[0] + bspan = GLOBALutils.calc_bss(vels,xc_av) + SP = bspan[0] if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} - moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':float(h[0].header['EXPTIME'])} + moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ + 'lunation':lunation,'mephem':mephem,'texp':float(h[0].header['EXPTIME'])} pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' + ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' - if not avoid_plot: - GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) + if not avoid_plot: + GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) SNR_5130 = np.median(spec[8,103 - difo,900:1101] ) airmass = float(h[0].header['AIRMASS']) seeing = -999 - TEXP = float(h[0].header['EXPTIME']) + TEXP = float(h[0].header['EXPTIME']) if sp_type == 'G2': - if T_eff < 6000: - A = 0.06544 - B = 0.00146 - D = 0.24416 - C = 0.00181 - else: - A = 0.09821 - B = 0.00014 - D = 0.33491 - C = 0.00113 + if T_eff < 6000: + A = 0.06544 + B = 0.00146 + D = 0.24416 + C = 0.00181 + else: + A = 0.09821 + B = 0.00014 + D = 0.33491 + C = 0.00113 elif sp_type == 'K5': - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 else: - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) - depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) + depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) - if depth_fact >= 1.: - RVerr2 = -999.000 - else: + if depth_fact >= 1.: + RVerr2 = -999.000 + else: if sp_type == 'G2': depth_fact = (1 - 0.62) / (1 - depth_fact) else: @@ -895,27 +896,27 @@ if (RVerr2 <= 0.009): RVerr2 = 0.009 - BSerr = D / float(np.round(SNR_5130)) + C + BSerr = D / float(np.round(SNR_5130)) + C - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr2,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 logg_err = 0.5 Z_err = 0.5 vsini_err = 2 - XC_min = np.abs(np.around(np.min(XCmodel),2)) + XC_min = np.abs(np.around(np.min(XCmodel),2)) - SNR_5130 = np.around(SNR_5130) - SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) + SNR_5130 = np.around(SNR_5130) + SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) - disp_epoch = np.around(p1gau_m[2],1) + disp_epoch = np.around(p1gau_m[2],1) hdu = GLOBALutils.update_header(hdu,'RV', RV) hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) hdu = GLOBALutils.update_header(hdu,'BS', BS) @@ -923,24 +924,24 @@ hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) - hdu = GLOBALutils.update_header(hdu,'INST', 'CAFE') - hdu = GLOBALutils.update_header(hdu,'RESOL', '70000') - hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') - hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) - hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) + hdu = GLOBALutils.update_header(hdu,'INST', 'CAFE') + hdu = GLOBALutils.update_header(hdu,'RESOL', '70000') + hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') + hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) + hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) # write to output line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f cafe ceres 70000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) - f_res.write(line_out) - + TEXP, SNR_5130_R, ccf_pdf) + f_res.write(line_out) + if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) else: - print "Reading spectral file from", fout + print("Reading spectral file from", fout) spec = pyfits.getdata( fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/cafe/cafeutils.py b/cafe/cafeutils.py index 1c886a1..c8a5018 100644 --- a/cafe/cafeutils.py +++ b/cafe/cafeutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from astropy.io import fits as pyfits import numpy as np import scipy @@ -23,68 +24,68 @@ def getObName(h): return obname def ra_from_sec(ra): - ra = float(ra) - sign = ' ' - if ra < 0: - sign = '-' - ra *= -1 - - hh = ra/3600. - mm = (hh - int(hh))*60. - ss = (mm - int(mm))*60. - shh = str(int(hh)) - smm = str(int(mm)) - sss = str(np.around(ss,2)) - if hh<10: - shh = '0' + shh - if mm<10: - smm = '0' + smm - if ss<10: - sss = '0' + sss - return sign + shh + ':' + smm + ':' + sss - + ra = float(ra) + sign = ' ' + if ra < 0: + sign = '-' + ra *= -1 + + hh = ra/3600. + mm = (hh - int(hh))*60. + ss = (mm - int(mm))*60. + shh = str(int(hh)) + smm = str(int(mm)) + sss = str(np.around(ss,2)) + if hh<10: + shh = '0' + shh + if mm<10: + smm = '0' + smm + if ss<10: + sss = '0' + sss + return sign + shh + ':' + smm + ':' + sss + def yesterday(date): - y,m,d = int(date[:4]),int(date[5:7]),int(date[8:]) - pd = d - 1 - pm = m - py = y - if pd == 0: - pm = m - 1 - py = y - if pm in [1,3,5,7,8,10,12]: - pd = 31 - elif pm in [4,6,9,11]: - pd = 30 - else: - pd = 28 - p,q,r = False,False,False - p = y%4 - q = y%100 - r = y%400 - if p == 0: - p = True - if q == 0: - q = True - if r == 0: - r = True - if p and (not q or r): - pd = 29 - - if pm == 0: - py = y - 1 - pm = 12 - pd = 31 - - sy,sm,sd = str(py),str(pm),str(pd) - if pm < 10: - sm = '0'+sm - if pd < 10: - sd = '0'+sd - return sy + '-' + sm + '-' + sd + y,m,d = int(date[:4]),int(date[5:7]),int(date[8:]) + pd = d - 1 + pm = m + py = y + if pd == 0: + pm = m - 1 + py = y + if pm in [1,3,5,7,8,10,12]: + pd = 31 + elif pm in [4,6,9,11]: + pd = 30 + else: + pd = 28 + p,q,r = False,False,False + p = y%4 + q = y%100 + r = y%400 + if p == 0: + p = True + if q == 0: + q = True + if r == 0: + r = True + if p and (not q or r): + pd = 29 + + if pm == 0: + py = y - 1 + pm = 12 + pd = 31 + + sy,sm,sd = str(py),str(pm),str(pd) + if pm < 10: + sm = '0'+sm + if pd < 10: + sd = '0'+sd + return sy + '-' + sm + '-' + sd def FileClassify(diri, log): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -105,62 +106,62 @@ def FileClassify(diri, log): #Do not consider the images specified in dir+badfiles.txt bad_files = [] if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + all_files = glob.glob(diri+"/*fits") for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - if h[0].header['IMAGETYP'] == 'Science': - sim_sci.append(archivo) - obname = getObName(h) - obnames.append( obname ) - ra = ra_from_sec(h[0].header['RA']) - delta = ra_from_sec(h[0].header['DEC']) - airmass= float(h[0].header['AIRMASS']) - texp = float(h[0].header['EXPTIME']) - - date = h[0].header['DATE'] - stime = h[0].header['UT_START'] - etime = h[0].header['UT_END'] - date = date[:10] - if stime > etime: - date = yesterday(date) - hour = ra_from_sec(stime) - exptimes.append( texp ) - line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) - f.write(line) - elif h[0].header['IMAGETYP'] == 'Bias': - biases.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - bias_ref_dates.append( mjd ) - elif h[0].header['IMAGETYP'] == 'Flat': - flats.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - flat_ref_dates.append( mjd ) - elif h[0].header['IMAGETYP'] == 'Calibration': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - ThAr_ref_dates.append( mjd ) - + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + if h[0].header['IMAGETYP'] == 'Science': + sim_sci.append(archivo) + obname = getObName(h) + obnames.append( obname ) + ra = ra_from_sec(h[0].header['RA']) + delta = ra_from_sec(h[0].header['DEC']) + airmass= float(h[0].header['AIRMASS']) + texp = float(h[0].header['EXPTIME']) + + date = h[0].header['DATE'] + stime = h[0].header['UT_START'] + etime = h[0].header['UT_END'] + date = date[:10] + if stime > etime: + date = yesterday(date) + hour = ra_from_sec(stime) + exptimes.append( texp ) + line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) + f.write(line) + elif h[0].header['IMAGETYP'] == 'Bias': + biases.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + bias_ref_dates.append( mjd ) + elif h[0].header['IMAGETYP'] == 'Flat': + flats.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + flat_ref_dates.append( mjd ) + elif h[0].header['IMAGETYP'] == 'Calibration': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + ThAr_ref_dates.append( mjd ) + flat_ref_dates = np.array(flat_ref_dates) flats = np.array(flats) IS = np.argsort(flat_ref_dates) flat_ref_dates = flat_ref_dates[IS] flats = flats[IS] #for i in range(len(flats)): - # print 'flat',flats[i], flat_ref_dates[i] + # print 'flat',flats[i], flat_ref_dates[i] bias_ref_dates = np.array(bias_ref_dates) biases = np.array(biases) @@ -168,7 +169,7 @@ def FileClassify(diri, log): bias_ref_dates = bias_ref_dates[IS] biases = biases[IS] #for i in range(len(biases)): - # print 'bias',biases[i], bias_ref_dates[i] + # print 'bias',biases[i], bias_ref_dates[i] f.close() return biases, np.array(flats), np.array(ThAr_ref), sim_sci, np.array(ThAr_ref_dates), obnames, exptimes @@ -189,13 +190,13 @@ def mjd_fromheader(h): """ secinday = 24*3600.0 - date = h[0].header['DATE'] + date = h[0].header['DATE'] stime = h[0].header['UT_START'] etime = h[0].header['UT_END'] date = date[:10] if stime > etime: - date = yesterday(date) + date = yesterday(date) hour = ra_from_sec(stime) datetu = date.replace('-',':') @@ -215,9 +216,9 @@ def mjd_fromheader2(h): secinday = 24*3600.0 date = h[0].header['DATE'] - hour = date[11:] + hour = date[11:] date = date[:10] - + datetu = date.replace('-',':') ut = hour.replace('-',':') @@ -255,9 +256,9 @@ def MedianCombine(ImgList, zero='none'): d1 = OverscanTrim(d1).astype('float') if zero != 'none': - z = pyfits.open(zero)[0] - d1 -= z.data.astype('float') - + z = pyfits.open(zero)[0] + d1 -= z.data.astype('float') + factor = 1.25 if (n < 3): factor = 1 @@ -266,14 +267,14 @@ def MedianCombine(ImgList, zero='none'): ron1 = factor * ron1 / np.sqrt(n) if n>1: - for i in range(n-1): - td = pyfits.open(ImgList[i+1]) - if zero == 'none': - d1 = np.dstack((d1,OverscanTrim(b_col(td[0].data)))) - - else: - d1 = np.dstack((d1,OverscanTrim(b_col(td[0].data))-z.data)) - d1 = np.median(d1,axis=2) + for i in range(n-1): + td = pyfits.open(ImgList[i+1]) + if zero == 'none': + d1 = np.dstack((d1,OverscanTrim(b_col(td[0].data)))) + + else: + d1 = np.dstack((d1,OverscanTrim(b_col(td[0].data))-z.data)) + d1 = np.median(d1,axis=2) return d1, ron1, gain1 @@ -297,6 +298,6 @@ def OverscanTrim(d): return newdata def b_col(d): - d[:,233] = 0.5*(d[:,232]+d[:,235]) - d[:,234] = 0.5*(d[:,232]+d[:,235]) - return d \ No newline at end of file + d[:,233] = 0.5*(d[:,232]+d[:,235]) + d[:,234] = 0.5*(d[:,232]+d[:,235]) + return d diff --git a/clean.py b/clean.py index 7d3e44c..f16dc5b 100644 --- a/clean.py +++ b/clean.py @@ -1,14 +1,15 @@ +from __future__ import print_function ######################## Clean Script v.1.0. ########################### # # # This script cleans all the subdirectories of the current directory # # os .so, .pyc, .something~ and build folders. USE WITH CAUTION; # # it was designed to be used in the ceres folder only. # # # -# # -# UPDATES: # -# # +# # +# UPDATES: # +# # # 02-09-2016: Finished code. Working like a charm! # -# # +# # ######################################################################## import glob @@ -20,24 +21,24 @@ def getDirs(foldername): def SeeknDestroy(directory): if(directory == 'utils/SSEphem'): - or_path = os.getcwd() - os.chdir('utils/SSEphem') - p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - os.system('rm *403*') - os.system('rm finals2000*') - os.system('rm iers*tab') - os.system('rm leap*tab') - os.system('rm *pyc') - os.system('rm alltimes asc2bin closureT delayT earthT ephemT gbxyz gc_fn_test ierspast jupiterT lst makeiers marsT mdy2mjd mercuryT mjd2mdy moonT moonvelT neptuneT nutT plutoT saturnT ssobject sunT topochk uranusT venusT') - os.chdir('SOFA') - p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - os.system('rm -r lib') - os.system('rm -r include') - os.system('rm t_sofa_c') - os.system('rm libsofa_c.a') - p.wait() - os.chdir(or_path) + or_path = os.getcwd() + os.chdir('utils/SSEphem') + p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + os.system('rm *403*') + os.system('rm finals2000*') + os.system('rm iers*tab') + os.system('rm leap*tab') + os.system('rm *pyc') + os.system('rm alltimes asc2bin closureT delayT earthT ephemT gbxyz gc_fn_test ierspast jupiterT lst makeiers marsT mdy2mjd mercuryT mjd2mdy moonT moonvelT neptuneT nutT plutoT saturnT ssobject sunT topochk uranusT venusT') + os.chdir('SOFA') + p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + os.system('rm -r lib') + os.system('rm -r include') + os.system('rm t_sofa_c') + os.system('rm libsofa_c.a') + p.wait() + os.chdir(or_path) # We obtain al files and folders of the current directory... files_and_folders = glob.glob(directory+'/*') # ...and we check each folder or file: @@ -45,20 +46,20 @@ def SeeknDestroy(directory): # We search for .so, .pyc, .something~ files or build folders. # If we find them, we erase them: if( cf[-3:] == '.so' or cf[-4:] == '.pyc' or cf[-1:] == '~'): - os.remove(cf) + os.remove(cf) elif( cf[-5:] == 'build' ): - shutil.rmtree(cf) - # If the current file or folder is a directory, we apply the same + shutil.rmtree(cf) + # If the current file or folder is a directory, we apply the same # SeeknDestroy code to it: elif( os.path.isdir(cf) ): - SeeknDestroy(cf) + SeeknDestroy(cf) # First, we get all the directories in the current folder: dirs1 = getDirs('.') #dirs2 = getDirs('utils') for directory in dirs1: - print directory + print(directory) # To each directory, we apply the SeeknDestroy function: SeeknDestroy(directory) #for directory in dirs2: diff --git a/coralie/coraliepipe.py b/coralie/coraliepipe.py index f4156bc..bbd55f5 100644 --- a/coralie/coraliepipe.py +++ b/coralie/coraliepipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") base = '../' @@ -105,7 +106,7 @@ NSigma_Marsh = 5 NCosmic_Marsh = 10 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 50 max_extract_col = 2000 n_useful = 70 # up to which order do we care? @@ -115,7 +116,7 @@ ncoef_m = 6 npar_wsol = (min(ncoef_x,ncoef_m) + 1) * (2*max(ncoef_x,ncoef_m) - min(ncoef_x,ncoef_m) + 2) / 2 -models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models +models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models order_dir = base+"coralie/wavcals/" # path to reference files for the wavelength solution ############################# @@ -123,15 +124,15 @@ # file containing the log log = dirout+'night.log' -print "\n\n\tCoralie Euler1.2m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tCoralie Euler1.2m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # classification of input images according to header info biases, ob_flats, co_flats, ob_loc, co_loc, ThAr_ref, ThFP_ref,\ simThAr_sci,sim_FP_sci,ThAr_ref_dates,ThFP_ref_dates,obnames,\ - obnames_FP,exptimes, exptimes_FP, flats = coralieutils.FileClassify(dirin,log) + obnames_FP,exptimes, exptimes_FP, flats = coralieutils.FileClassify(dirin,log) # Pre-process if ( (( len(ob_flats) > 0) and (os.access(dirout+'FlatOb.fits',os.F_OK) == False)) or \ @@ -140,84 +141,84 @@ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): - print "\tGenerating Master calibration frames..." + print("\tGenerating Master calibration frames...") # median combine Biases MasterBias, RO_bias, GA_bias = coralieutils.MedianCombine(biases,ZF=0.) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") if len(flats) > 0: - # median combine list of co flats2 - Flat,RO_flat,GA_flat = coralieutils.MedianCombine(flats,ZF=MasterBias) - hdu = pyfits.PrimaryHDU(Flat) - if (os.access(dirout+'Flat.fits',os.F_OK)): - os.remove(dirout+'Flat.fits') - hdu.writeto(dirout+'Flat.fits') + # median combine list of co flats2 + Flat,RO_flat,GA_flat = coralieutils.MedianCombine(flats,ZF=MasterBias) + hdu = pyfits.PrimaryHDU(Flat) + if (os.access(dirout+'Flat.fits',os.F_OK)): + os.remove(dirout+'Flat.fits') + hdu.writeto(dirout+'Flat.fits') if len(ob_flats) > 0: - # median combine list of ob flats - Flat_ob, RO_ob, GA_ob = coralieutils.MedianCombine(ob_flats,ZF=MasterBias) - # save this file for later reference - hdu = pyfits.PrimaryHDU( Flat_ob ) - if (os.access(dirout+'FlatOb.fits',os.F_OK)): - os.remove(dirout+'FlatOb.fits') - hdu.writeto(dirout+'FlatOb.fits') + # median combine list of ob flats + Flat_ob, RO_ob, GA_ob = coralieutils.MedianCombine(ob_flats,ZF=MasterBias) + # save this file for later reference + hdu = pyfits.PrimaryHDU( Flat_ob ) + if (os.access(dirout+'FlatOb.fits',os.F_OK)): + os.remove(dirout+'FlatOb.fits') + hdu.writeto(dirout+'FlatOb.fits') else: - Flat_ob = Flat + Flat_ob = Flat if len(co_flats) > 0: - # median combine list of co flats - Flat_co,RO_co,GA_co = coralieutils.MedianCombine(co_flats,ZF=MasterBias) - hdu = pyfits.PrimaryHDU(Flat_co) - if (os.access(dirout+'FlatCo.fits',os.F_OK)): - os.remove(dirout+'FlatCo.fits') - hdu.writeto(dirout+'FlatCo.fits') + # median combine list of co flats + Flat_co,RO_co,GA_co = coralieutils.MedianCombine(co_flats,ZF=MasterBias) + hdu = pyfits.PrimaryHDU(Flat_co) + if (os.access(dirout+'FlatCo.fits',os.F_OK)): + os.remove(dirout+'FlatCo.fits') + hdu.writeto(dirout+'FlatCo.fits') else: - Flat_co = Flat + Flat_co = Flat - print "\t\t-> Masterflats: done!" + print("\t\t-> Masterflats: done!") # Find orders & traces - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") if len(ob_flats)>0 and len(co_flats)>0: - c_ob, nord_ob = GLOBALutils.get_them(Flat_ob, 8, trace_degree,maxords=-1,mode=1) - c_co, nord_co = GLOBALutils.get_them(Flat_co, 8, trace_degree,maxords=-1,startfrom=300,mode=1) + c_ob, nord_ob = GLOBALutils.get_them(Flat_ob, 8, trace_degree,maxords=-1,mode=1) + c_co, nord_co = GLOBALutils.get_them(Flat_co, 8, trace_degree,maxords=-1,startfrom=300,mode=1) else: c_all, nord_all = GLOBALutils.get_them(Flat, 5, trace_degree,maxords=-1,mode=1,nsigmas=3) - GA_co,GA_ob = GA_flat, GA_flat - RO_co,RO_ob = RO_flat, RO_flat + GA_co,GA_ob = GA_flat, GA_flat + RO_co,RO_ob = RO_flat, RO_flat - c_ob = c_all[:22] - c_co = c_all[22] - i = 23 - while i < len(c_all)-1: - c_ob = np.vstack((c_ob,c_all[i])) - c_co = np.vstack((c_co,c_all[i+1])) - i+=2 - nord_co, nord_ob = len(c_co),len(c_ob) - print '\t', nord_ob, 'object orders found...' - print '\t', nord_co, 'comparison orders found...' + c_ob = c_all[:22] + c_co = c_all[22] + i = 23 + while i < len(c_all)-1: + c_ob = np.vstack((c_ob,c_all[i])) + c_co = np.vstack((c_co,c_all[i+1])) + i+=2 + nord_co, nord_ob = len(c_co),len(c_ob) + print('\t', nord_ob, 'object orders found...') + print('\t', nord_co, 'comparison orders found...') if len(ob_flats)>0 and len(co_flats)>0: - trace_dict = {'c_ob':c_ob,'c_co':c_co, + trace_dict = {'c_ob':c_ob,'c_co':c_co, 'nord_ob':nord_ob, 'nord_co':nord_co, 'GA_ob': GA_ob, 'RO_ob': RO_ob, 'GA_co': GA_co, 'RO_co': RO_co} else: - trace_dict = {'c_all':c_all,'c_ob':c_ob,'c_co':c_co, + trace_dict = {'c_all':c_all,'c_ob':c_ob,'c_co':c_co, 'nord_ob':nord_ob, 'nord_co':nord_co,'nord_all':nord_all, 'GA_ob': GA_ob, 'RO_ob': RO_ob, 'GA_co': GA_co, 'RO_co': RO_co} @@ -240,25 +241,25 @@ RO_co = trace_dict['RO_co'] # recover flats & master bias if len(ob_flats)>0: - h = pyfits.open(dirout+'FlatOb.fits') - Flat_ob = h[0].data + h = pyfits.open(dirout+'FlatOb.fits') + Flat_ob = h[0].data else: - h = pyfits.open(dirout+'Flat.fits') - Flat_ob = h[0].data + h = pyfits.open(dirout+'Flat.fits') + Flat_ob = h[0].data if len(co_flats)>0: h = pyfits.open(dirout+'Flat.fits') Flat_co = h[0].data else: - h = pyfits.open(dirout+'Flat.fits') - Flat_co = h[0].data + h = pyfits.open(dirout+'Flat.fits') + Flat_co = h[0].data h = pyfits.open(dirout+'MasterBias.fits') MasterBias = h[0].data if len(ob_flats)>0 and len(co_flats)>0: - c_all = GLOBALutils.Mesh(c_ob,c_co) + c_all = GLOBALutils.Mesh(c_ob,c_co) -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') # Extract flat spectra, object P_ob_fits = dirout + 'P_ob.fits' S_flat_ob_fits = dirout +'S_flat_ob.fits' @@ -267,9 +268,9 @@ if ( os.access(P_ob_fits,os.F_OK) == False ) or ( os.access(S_flat_ob_fits,os.F_OK) == False ) or \ (force_flat_extract): - print "\t\tNo extracted flat object spectra found or extraction forced, extracting and saving..." - - print "\t\t\tWill extract",nord_ob,"orders for object fibre..." + print("\t\tNo extracted flat object spectra found or extraction forced, extracting and saving...") + + print("\t\t\tWill extract",nord_ob,"orders for object fibre...") P_ob = GLOBALutils.obtain_P(Flat_ob,c_ob,ext_aperture,RO_ob,\ GA_ob,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_col,\ @@ -284,15 +285,15 @@ os.remove( P_ob_fits ) if (os.access(S_flat_ob_fits,os.F_OK)): os.remove( S_flat_ob_fits ) - + hdu = pyfits.PrimaryHDU( P_ob ) hdu.writeto( P_ob_fits ) hdu = pyfits.PrimaryHDU( S_flat_ob ) hdu.writeto( S_flat_ob_fits ) - + else: - print "\t\tExtracted flat object spectra found, loading..." + print("\t\tExtracted flat object spectra found, loading...") P_ob = pyfits.getdata( P_ob_fits ) S_flat_ob = pyfits.getdata( S_flat_ob_fits ) @@ -302,17 +303,17 @@ P_co = np.zeros( Flat_co.shape ) S_flat_co = np.zeros((nord_co, 3, Flat_co.shape[1]) ) if ( os.access(P_co_fits,os.F_OK) == False ) or ( os.access(S_flat_co_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving..." - - print "\t\t\tWill extract",nord_co,"orders for comparison fibre" + print("\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving...") + + print("\t\t\tWill extract",nord_co,"orders for comparison fibre") P_co = GLOBALutils.obtain_P(Flat_co,c_co,ext_aperture,RO_co,\ GA_co,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_col,\ max_extract_col, npools) - + S_flat_co = GLOBALutils.optimal_extraction(Flat_co,P_co,c_co,ext_aperture,RO_co,GA_co,\ S_Marsh,NCosmic_Marsh,min_extract_col,\ - max_extract_col,npools) + max_extract_col,npools) # write P_on and S_flat_co as fits files if (os.access(P_co_fits,os.F_OK)): @@ -324,9 +325,9 @@ hdu.writeto( P_co_fits ) hdu = pyfits.PrimaryHDU( S_flat_co ) hdu.writeto( S_flat_co_fits ) - + else: - print "\t\tExtracted flat comparison spectra found, loading..." + print("\t\tExtracted flat comparison spectra found, loading...") P_co = pyfits.getdata( P_co_fits ) S_flat_co = pyfits.getdata( S_flat_co_fits ) @@ -334,7 +335,7 @@ S_flat_ob_n, maxvals_ob = GLOBALutils.FlatNormalize_single( S_flat_ob, mid=int(0.5*S_flat_ob.shape[2])) S_flat_co_n, maxvals_co = GLOBALutils.FlatNormalize_single( S_flat_co, mid=int(0.5*S_flat_co.shape[2])) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in ThAr_ref: hthar = pyfits.open( fsim ) @@ -342,31 +343,31 @@ ron = hthar[0].header['HIERARCH ESO CORA CCD RON'] gain = hthar[0].header['HIERARCH ESO CORA CCD GAIN'] thar_fits_ob = dirout + fsim.split('/')[-1][:-8]+'spec.ob.fits.S' - thar_fits_co = dirout + fsim.split('/')[-1][:-8]+'spec.co.fits.S' + thar_fits_co = dirout + fsim.split('/')[-1][:-8]+'spec.co.fits.S' if ( os.access(thar_fits_ob,os.F_OK) == False ) or \ ( os.access(thar_fits_co,os.F_OK) == False ) or \ (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." - thar_S_ob = GLOBALutils.optimal_extraction(dthar,P_ob,c_ob,ext_aperture,ron,gain,\ + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") + thar_S_ob = GLOBALutils.optimal_extraction(dthar,P_ob,c_ob,ext_aperture,ron,gain,\ + S_Marsh,100.,min_extract_col,max_extract_col,npools) + thar_S_co = GLOBALutils.optimal_extraction(dthar,P_co,c_co,ext_aperture,ron,gain,\ S_Marsh,100.,min_extract_col,max_extract_col,npools) - thar_S_co = GLOBALutils.optimal_extraction(dthar,P_co,c_co,ext_aperture,ron,gain,\ - S_Marsh,100.,min_extract_col,max_extract_col,npools) - + # save as fits file if (os.access(thar_fits_ob,os.F_OK)): os.remove( thar_fits_ob ) if (os.access(thar_fits_co,os.F_OK)): os.remove( thar_fits_co ) - + hdu = pyfits.PrimaryHDU( thar_S_ob ) hdu.writeto( thar_fits_ob ) hdu = pyfits.PrimaryHDU( thar_S_co ) hdu.writeto( thar_fits_co ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") # compute wavelength calibration files sorted_ThAr_dates = np.argsort( ThAr_ref_dates ) p0_array = np.zeros( (len(ThAr_ref_dates), npar_wsol) ) @@ -377,7 +378,7 @@ thar_fits_ob = dirout + ThAr_ref[index].split('/')[-1][:-8]+'spec.ob.fits.S' thar_fits_co = dirout + ThAr_ref[index].split('/')[-1][:-8]+'spec.co.fits.S' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tComputing wavelength solution of ThAr file", ThAr_ref[index] + print("\t\tComputing wavelength solution of ThAr file", ThAr_ref[index]) hthar = pyfits.open( ThAr_ref[index] ) mjd, mjd0 = coralieutils.mjd_fromheader( hthar ) thar_S_ob = pyfits.getdata( thar_fits_ob ) @@ -399,11 +400,11 @@ order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar_ob[order,:] IV = iv_thar_ob[order,:] wei = np.sqrt( IV ) - bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ @@ -412,7 +413,7 @@ thar_order,order,wei,rmsmax=5000000,\ minlines=10,FixEnds=True,Dump_Argon=dumpargon,\ Dump_AllLines=True, Cheby=use_cheby) - if (order == 35): + if (order == 35): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) else: @@ -426,7 +427,7 @@ All_Intensities = np.append( All_Intensities, intensities ) p0 = np.zeros( npar_wsol ) - p0[0] = (35+89) * Global_ZP + p0[0] = (35+89) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ @@ -445,18 +446,18 @@ order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar_co[order-22,:] IV = iv_thar_co[order-22,:] wei = np.sqrt( IV ) - bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig - bkg - + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+'o.iwdat', thar_order, order, wei, \ rmsmax=5000000, minlines=10,FixEnds=True,Dump_Argon=dumpargon, \ Dump_AllLines=True, Cheby=use_cheby) - + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order ) @@ -468,37 +469,37 @@ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ np.ones(All_Intensities_co.shape), p1, Cheby=use_cheby,\ maxrms=MRMS, Inv=Inverse_m,minlines=500,order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - # end COMPARISON orders. + + # end COMPARISON orders. pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas, 'p1_co':p1_co, 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, 'G_wav_co':G_wav_co, 'II_co':II_co, 'rms_ms_co':rms_ms_co,\ 'G_res_co':G_res_co, 'All_Centroids_co':All_Centroids_co} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) - #print "Median sigma:", np.median( All_Sigmas ) + #print "Median sigma:", np.median( All_Sigmas ) p0_array[i,:] = p1 else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl,'r')) p0_array[i,:] = pdict['p1'] p0_G = np.median(p0_array,axis=0) if len(ThFP_ref) > 0: - print '\n\tExtraction of Fabry-Perot calibration frames:' + print('\n\tExtraction of Fabry-Perot calibration frames:') else: - print '\n\tNo Fabry-Perot calibration images found, moving on' + print('\n\tNo Fabry-Perot calibration images found, moving on') # Now extract ThAr-FP images for fsim in ThFP_ref: hthfp = pyfits.open( fsim ) thfp_fits_ob = dirout + fsim.split('/')[-1][:-8]+'spec.ob.fits.S' thfp_fits_co = dirout + fsim.split('/')[-1][:-8]+'spec.co.fits.S' - + if ( os.access(thfp_fits_ob,os.F_OK) == False ) or \ ( os.access(thfp_fits_co,os.F_OK) == False ) or \ (force_thfp_extract): - print "\t\tNo previous extraction or extraction forced for ThFP file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThFP file", fsim, "extracting...") dthfp = coralieutils.OverscanTrim( pyfits.getdata( fsim ) ) Centers = np.zeros((len(c_all),dthfp.shape[1])) for i in range(c_all.shape[0]): @@ -506,11 +507,11 @@ bac = GLOBALutils.get_scat(dthfp,Centers,span=5) dthfp -= bac - thfp_S_ob = GLOBALutils.optimal_extraction(dthfp,P_ob,c_ob,ext_aperture,\ + thfp_S_ob = GLOBALutils.optimal_extraction(dthfp,P_ob,c_ob,ext_aperture,\ hthfp[0].header['HIERARCH ESO CORA CCD RON'],\ hthfp[0].header['HIERARCH ESO CORA CCD GAIN'],\ S_Marsh,100.,min_extract_col,max_extract_col,npools) - thfp_S_co = GLOBALutils.optimal_extraction(dthfp,P_co,c_co,ext_aperture,\ + thfp_S_co = GLOBALutils.optimal_extraction(dthfp,P_co,c_co,ext_aperture,\ hthfp[0].header['HIERARCH ESO CORA CCD RON'], hthfp[0].header['HIERARCH ESO CORA CCD GAIN'], S_Marsh,100.,min_extract_col,max_extract_col,npools) @@ -519,16 +520,16 @@ os.remove( thfp_fits_ob ) if (os.access(thfp_fits_co,os.F_OK)): os.remove( thfp_fits_co ) - + hdu = pyfits.PrimaryHDU( thfp_S_ob ) hdu.writeto( thfp_fits_ob ) hdu = pyfits.PrimaryHDU( thfp_S_co ) hdu.writeto( thfp_fits_co ) else: - print "\t\tFP file", fsim, "all ready extracted, loading..." + print("\t\tFP file", fsim, "all ready extracted, loading...") # Now calibrate the ThFP spectra with the closest ThAr spectrum -print '\n\tWavelength solution of Fabry-Perot spectra with closest ThAr spectrum:' +print('\n\tWavelength solution of Fabry-Perot spectra with closest ThAr spectrum:') for fsim in ThFP_ref: hthfp = pyfits.open( fsim ) mjd, mjd0 = coralieutils.mjd_fromheader(hthfp) @@ -538,57 +539,57 @@ thfp_fits_co = dirout + fsim.split('/')[-1][:-8]+'spec.co.fits.S' wavsol_pkl_fp = dirout + fsim.split('/')[-1][:-8]+'wavsolpars.pkl' if ( os.access(wavsol_pkl_fp,os.F_OK) == False ) or (force_thfp_wavcal): - print '\t\tCalibrating', fsim,'...' - thar_fp = pyfits.getdata(thfp_fits_ob) - lines_thar_ob = thar_fp[:,1,:] - iv_thar_ob = thar_fp[:,2,:] - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - - for order in range(n_useful): - order_s = str(order) - if (order < 10): - order_s = '0'+str(order) - thar_order_orig = lines_thar_ob[order,:] - IV = iv_thar_ob[order,:] - wei = np.sqrt( IV ) - bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+'o.iwdat', thar_order, order, wei, \ - rmsmax=5000000, minlines=10,FixEnds=True,Dump_Argon=dumpargon, \ - Dump_AllLines=True, Cheby=use_cheby) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ - np.ones(All_Intensities.shape), p0_G, Cheby=use_cheby,\ - maxrms=100, Inv=Inverse_m, minlines=minlines_glob_ob,\ - order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + print('\t\tCalibrating', fsim,'...') + thar_fp = pyfits.getdata(thfp_fits_ob) + lines_thar_ob = thar_fp[:,1,:] + iv_thar_ob = thar_fp[:,2,:] - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix, G_wav, G_ord,\ - np.ones(G_wav.shape), wavsol_dict['p1'],\ - Cheby=True,Inv=True,maxrms=100,minlines=minlines_glob_ob,\ - order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) - pdict = {'p1':p1,'p_shift':p_shift,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ - 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas, 'p1_co':wavsol_dict['p1_co']} - pickle.dump( pdict, open( wavsol_pkl_fp, 'w' ) ) + for order in range(n_useful): + order_s = str(order) + if (order < 10): + order_s = '0'+str(order) + thar_order_orig = lines_thar_ob[order,:] + IV = iv_thar_ob[order,:] + wei = np.sqrt( IV ) + bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+'o.iwdat', thar_order, order, wei, \ + rmsmax=5000000, minlines=10,FixEnds=True,Dump_Argon=dumpargon, \ + Dump_AllLines=True, Cheby=use_cheby) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ + np.ones(All_Intensities.shape), p0_G, Cheby=use_cheby,\ + maxrms=100, Inv=Inverse_m, minlines=minlines_glob_ob,\ + order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix, G_wav, G_ord,\ + np.ones(G_wav.shape), wavsol_dict['p1'],\ + Cheby=True,Inv=True,maxrms=100,minlines=minlines_glob_ob,\ + order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + pdict = {'p1':p1,'p_shift':p_shift,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ + 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas, 'p1_co':wavsol_dict['p1_co']} + pickle.dump( pdict, open( wavsol_pkl_fp, 'w' ) ) else: - print '\t\tFP spectrum', fsim, 'already calibrated, loading...' + print('\t\tFP spectrum', fsim, 'already calibrated, loading...') ### start of science frame reductions ### @@ -610,9 +611,9 @@ new_list_obnames.append( obname ) new_list_texp.append( texp ) -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') for nlisti in range(len(new_list)): - print '\t\t'+new_list_obnames[nlisti] + print('\t\t'+new_list_obnames[nlisti]) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -634,7 +635,7 @@ use_moon = np.array(use_moon) # now extract the images -for nlisti in range(len(new_list)): +for nlisti in range(len(new_list)): fsim = new_list[ nlisti ] obname = new_list_obnames[ nlisti ] TEXP = np.around(new_list_texp[ nlisti ]) @@ -647,19 +648,19 @@ # get header h of image h = pyfits.open(fsim) - print '\n' - print "\t--> Working on image: ", fsim - + print('\n') + print("\t--> Working on image: ", fsim) + # get mjd and mjd0 mjd,mjd0 = coralieutils.mjd_fromheader(h) - # get gain and readnoise of object + # get gain and readnoise of object ronoise = h[0].header['HIERARCH ESO CORA CCD RON'] gain = h[0].header['HIERARCH ESO CORA CCD GAIN'] - + # Object name - print "\t\tObject name:",obname + print("\t\tObject name:",obname) # Open file, trim, overscan subtract and MasterBias subtract data = h[0].data @@ -685,7 +686,7 @@ ra = ra2 dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') # Find lambda_bary/lambda_topo using JPLEPHEM altitude = h[0].header['HIERARCH ESO OBS GEO ALTITUDE'] @@ -704,15 +705,15 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) - res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name = 'Swiss1.2' - gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs = ephem.Observer() + gobs.name = 'Swiss1.2' + gobs.lat = rad(latitude) # lat/long in decimal degrees gobs.long = rad(longitude) DDATE = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] @@ -741,12 +742,12 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sorted_indices = np.argsort( np.abs( np.array(ThAr_ref_dates) - mjd ) ) sorted_indices_FP = np.argsort( np.abs( np.array(ThFP_ref_dates) - mjd ) ) - print '\t\tExtraction:' + print('\t\tExtraction:') # optimally and simply extract spectra sci_fits_ob = dirout + fsim.split('/')[-1][:-8]+'spec.ob.fits.S' sci_fits_co = dirout + fsim.split('/')[-1][:-8]+'spec.co.fits.S' @@ -760,7 +761,7 @@ ( os.access(sci_fits_bac,os.F_OK) == False ) or \ (force_sci_extract): - print "\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") sci_Ss_ob = GLOBALutils.simple_extraction(data,c_ob,ext_aperture,\ min_extract_col,max_extract_col,npools) sci_Ss_co = GLOBALutils.simple_extraction(data,c_co,ext_aperture,\ @@ -776,7 +777,7 @@ S_Marsh,2.*NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) sci_bac = GLOBALutils.simple_extraction(bac,c_ob,ext_aperture,\ - min_extract_col,max_extract_col,npools) + min_extract_col,max_extract_col,npools) # save as fits file if (os.access(sci_fits_ob,os.F_OK)): @@ -788,7 +789,7 @@ if (os.access(sci_fits_co_simple,os.F_OK)): os.remove( sci_fits_co_simple ) if (os.access(sci_fits_bac,os.F_OK)): - os.remove( sci_fits_bac ) + os.remove( sci_fits_bac ) hdu = pyfits.PrimaryHDU( sci_S_ob ) hdu.writeto( sci_fits_ob ) hdu = pyfits.PrimaryHDU( sci_S_co ) @@ -800,13 +801,13 @@ hdu = pyfits.PrimaryHDU( sci_bac ) hdu.writeto( sci_fits_bac ) else: - print '\t\t\t'+fsim, "has already been extracted, reading in product fits files..." + print('\t\t\t'+fsim, "has already been extracted, reading in product fits files...") sci_S_ob = pyfits.getdata( sci_fits_ob ) sci_S_co = pyfits.getdata( sci_fits_co ) sci_Ss_ob = pyfits.getdata( sci_fits_ob_simple ) sci_Ss_co = pyfits.getdata( sci_fits_ob_simple ) sci_bac = pyfits.getdata( sci_fits_bac ) - + fout = 'proc/'+ obname + '_' + \ h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] + '_' +\ 'UT' + fsim[-17:-9] + '_' +\ @@ -825,7 +826,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['HIERARCH ESO OBS TEXP']) hdu = GLOBALutils.update_header(hdu,'HIERARCH FLUX WEIGHTED MEAN F ',h[0].header['HIERARCH ESO CORA PM FLUX TMMEAN']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['HIERARCH ESO TEL TARG ALPHA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['HIERARCH ESO TEL TARG DELTA']) @@ -839,15 +840,15 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',h[0].header['HIERARCH ESO OBS GEO ALTITUDE']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',h[0].header['HIERARCH ESO OBS TARG AIRMASS']) - print '\t\tWavelength calibration:' - print '\t\t\tComparision fibre is '+ h[0].header['HIERARCH ESO TPL TYPE'] + print('\t\tWavelength calibration:') + print('\t\t\tComparision fibre is '+ h[0].header['HIERARCH ESO TPL TYPE']) if h[0].header['HIERARCH ESO TPL TYPE'] == 'OBTH': # get ThAr closest in time indice = sorted_indices[0] thar_fits_ob = dirout + ThAr_ref[indice].split('/')[-1][:-8]+'spec.ob.fits.S' thar_fits_co = dirout + ThAr_ref[indice].split('/')[-1][:-8]+'spec.co.fits.S' pkl_wsol = dirout + ThAr_ref[indice].split('/')[-1][:-8]+'wavsolpars.pkl' - print "\t\t\tUnpickling reference wavelength solution from", pkl_wsol, " ..." + print("\t\t\tUnpickling reference wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) # Extract thAr lines from comparison orders lines_thar_co = sci_S_co[:,1,:] @@ -865,45 +866,45 @@ thar_order_orig = lines_thar_co[order-22,:] IV = iv_thar_co[order-22,:] wei = np.sqrt( IV ) - bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + bkg = GLOBALutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig - bkg - + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+'o.iwdat', thar_order, order, wei, \ - rmsmax=5000000, minlines=10,FixEnds=True,Dump_Argon=dumpargon, \ - Dump_AllLines=True, Cheby=use_cheby) - + rmsmax=5000000, minlines=10,FixEnds=True,Dump_Argon=dumpargon, \ + Dump_AllLines=True, Cheby=use_cheby) + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order ) All_Centroids_co = np.append( All_Centroids_co, centroids) All_Sigmas_co = np.append( All_Sigmas_co, sigmas) All_Intensities_co = np.append( All_Intensities_co, intensities ) - + # get a global solution for the lines found p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ - np.ones(All_Intensities_co.shape), wsol_dict['p1_co'], Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_co,\ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ + np.ones(All_Intensities_co.shape), wsol_dict['p1_co'], Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_co,\ order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - + # get shift with respect to reference ThAr p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co, G_wav_co, G_ord_co,\ - np.ones(G_wav_co.shape), wsol_dict['p1_co'],\ - Cheby=True,Inv=True,maxrms=100,minlines=minlines_glob_co,\ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co, G_wav_co, G_ord_co,\ + np.ones(G_wav_co.shape), wsol_dict['p1_co'],\ + Cheby=True,Inv=True,maxrms=100,minlines=minlines_glob_co,\ order0=89,ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - + precision = rms_ms/np.sqrt(len(I)) good_quality = True if (precision > 10): good_quality = False - + else: indice = sorted_indices_FP[0] thfp_fits_co = dirout + ThFP_ref[indice].split('/')[-1][:-8]+'spec.co.fits.S' pkl_wsol = dirout + ThFP_ref[indice].split('/')[-1][:-8]+'wavsolpars.pkl' - print "\t\t\tUnpickling reference wavelength solution from", pkl_wsol, " ..." + print("\t\t\tUnpickling reference wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) lines_thar_co = np.zeros(sci_Ss_co.shape) lines_thar_co_ref = np.zeros(sci_Ss_co.shape) @@ -947,10 +948,10 @@ p_sh = wsol_dict['p_shift'] * 299792458. * 1e-6 fp_shift += p_sh p_shift = 1e6*fp_shift/299792458. - print '\t\t\tFP shift = ',fp_shift[0],'+-',np.sqrt(np.var(rv_fps))/np.sqrt(float(len(rv_fps))),'m/s' + print('\t\t\tFP shift = ',fp_shift[0],'+-',np.sqrt(np.var(rv_fps))/np.sqrt(float(len(rv_fps))),'m/s') good_quality = True - equis = np.arange( data.shape[1] ) + equis = np.arange( data.shape[1] ) for order in range(n_useful): m = order + 89 @@ -980,7 +981,7 @@ nJ2 = np.where(np.isinf(spec[5,order])==True)[0] spec[5,order,nJ] = 1.0 spec[5,order,nJ2] = 1.0 - ratio = spec[3,order,:][L] / norm_spec[order][L] + ratio = spec[3,order,:][L] / norm_spec[order][L] spec[6,order,:][L] = spec[4,order,:][L] * (ratio ** 2 ) spec[7,order,:][L] = ratio #spec[8,order,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + ext_aperture*2*(ron/gain)**2 + sci_bac[order,:][L] / gain ) @@ -1004,13 +1005,13 @@ spec[6,order,:][LL] = spec[8,order,:][LL] ** 2 spec[6,order,:][LL2] = spec[8,order,:][LL2] ** 2 - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) if (not JustExtract): if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') # spectral analysis # First, query SIMBAD with the object name query_success = False @@ -1025,7 +1026,7 @@ pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./60000**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -1037,10 +1038,10 @@ f.write(line) f.close() else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -1056,16 +1057,16 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= (av_m - ml_v) @@ -1086,9 +1087,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): # first rough correlation to find the minimum @@ -1097,7 +1098,7 @@ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - # Normalize the continuum of the CCF robustly with lowess + # Normalize the continuum of the CCF robustly with lowess yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) @@ -1122,7 +1123,7 @@ xc_av /= pred moonsep_cor = h[0].header['HIERARCH ESO OBS MOON SEP'] - + if sp_type == 'M5': moon_sig = 2.5 elif sp_type == 'K5': @@ -1154,23 +1155,23 @@ #print 'Bisector span:', SP if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) @@ -1183,7 +1184,7 @@ SNR_5130 = np.median(spec[8,30,1000:1101] ) airmass = h[0].header['HIERARCH ESO OBS TARG AIRMASS'] seeing = h[0].header['HIERARCH ESO OBS AMBI DIMM SEEING'] - + if sp_type == 'G2': if T_eff < 6000: A = 0.06544 @@ -1197,12 +1198,12 @@ C = 0.00113 elif sp_type == 'K5': A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 else: A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 @@ -1225,13 +1226,13 @@ BSerr = D / float(np.round(SNR_5130)) + C - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr2,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1256,7 +1257,7 @@ hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f coralie ceres 60000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ TEXP, SNR_5130_R, ccf_pdf) @@ -1266,7 +1267,7 @@ hdu.writeto( dirout + fout ) else: - print "\t\tReading spectral file from", fout + print("\t\tReading spectral file from", fout) spec = pyfits.getdata( fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/coralie/coralieutils.py b/coralie/coralieutils.py index ec60252..c0e6548 100644 --- a/coralie/coralieutils.py +++ b/coralie/coralieutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use("Agg") from astropy.io import fits as pyfits @@ -30,7 +31,7 @@ def MedianCombine(ImgList,ZF=0.): h = pyfits.open(ImgList[0])[0] d = h.data d = OverscanTrim(d) - ZF - + factor = 1.25 if (n < 3): factor = 1 @@ -85,17 +86,17 @@ def getObName(h): values = [ ("scriptFIle", (pycurl.FORM_FILE, tfile)) ] - output = StringIO.StringIO() + output = StringIO() c = pycurl.Curl() - + c.setopt(pycurl.URL, "http://simbad.harvard.edu/simbad/sim-script") c.setopt(c.HTTPPOST, values) - c.setopt(pycurl.WRITEFUNCTION, output.write) + c.setopt(pycurl.WRITEFUNCTION, output.write) c.perform() c.close() - - result = output.getvalue() - lines = result.split('\n') + + result = output.getvalue() + lines = result.split('\n') result = lines[len(lines)-3] if (result.count('No') > 0): # build alternate obname based on ra-dec @@ -116,7 +117,7 @@ def getObName(h): def FileClassify(dir, log): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -142,57 +143,57 @@ def FileClassify(dir, log): bad_files = [] if os.access(dir+'bad_files.txt',os.F_OK): - bf = open(dir+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(dir+line[:-1]) - bf.close() - + bf = open(dir+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(dir+line[:-1]) + bf.close() + all_files = glob.glob(dir+"/CORALIE*fits") for archivo in all_files: - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - if h[0].header['HIERARCH ESO TPL TYPE'] == 'OBTH' or h[0].header['HIERARCH ESO TPL TYPE'] == 'OBFP': - obname = getObName(h) - ra = h[0].header['HIERARCH ESO OBS ALPHACAT'] - delta = h[0].header['HIERARCH ESO OBS DELTACAT'] - airmass= h[0].header['HIERARCH ESO OBS TARG AIRMASS'] - texp = h[0].header['HIERARCH ESO OBS TEXP'] - date = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] - hour = h[0].header['HIERARCH ESO CORA SHUTTER START HOUR'] - line = "%-15s %10s %10s %8.2f %4.2f %8s %7.4f %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) - f.write(line) - simThAr_sci.append(archivo) - obnames.append( obname ) - exptimes.append( texp ) - - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'BIAS': - biases.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FFO': - ob_flats.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FFC': - co_flats.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FF2': - flats.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'LOCO': - ob_loc.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'LOCC': - co_loc.append(archivo) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'THA2': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - ThAr_ref_dates.append( mjd ) - elif h[0].header['HIERARCH ESO TPL TYPE'] == 'THFP': - FP_ref.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - ThFP_ref_dates.append( mjd ) + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + if h[0].header['HIERARCH ESO TPL TYPE'] == 'OBTH' or h[0].header['HIERARCH ESO TPL TYPE'] == 'OBFP': + obname = getObName(h) + ra = h[0].header['HIERARCH ESO OBS ALPHACAT'] + delta = h[0].header['HIERARCH ESO OBS DELTACAT'] + airmass= h[0].header['HIERARCH ESO OBS TARG AIRMASS'] + texp = h[0].header['HIERARCH ESO OBS TEXP'] + date = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] + hour = h[0].header['HIERARCH ESO CORA SHUTTER START HOUR'] + line = "%-15s %10s %10s %8.2f %4.2f %8s %7.4f %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) + f.write(line) + simThAr_sci.append(archivo) + obnames.append( obname ) + exptimes.append( texp ) + + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'BIAS': + biases.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FFO': + ob_flats.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FFC': + co_flats.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'FF2': + flats.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'LOCO': + ob_loc.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'LOCC': + co_loc.append(archivo) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'THA2': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + ThAr_ref_dates.append( mjd ) + elif h[0].header['HIERARCH ESO TPL TYPE'] == 'THFP': + FP_ref.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + ThFP_ref_dates.append( mjd ) f.close() return biases, ob_flats, co_flats, ob_loc, co_loc, ThAr_ref, FP_ref, simThAr_sci, simFP_sci, ThAr_ref_dates, ThFP_ref_dates, obnames, obnames_FP, exptimes, exptimes_FP,flats @@ -201,8 +202,8 @@ def mjd_fromheader(h): """ return modified Julian date from header """ - - datetu = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] + + datetu = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] ut = h[0].header['HIERARCH ESO CORA SHUTTER START HOUR'] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[4:6]),int(datetu[6:8])) @@ -224,50 +225,50 @@ def XC_Final_Fit_Rot( X, Y, ldc = 0.8, vsini = 10.0 ): """ f0 = 0.1 vel0 = X[len(X)/2] - + def conv(x,g,x1,r1): - xi = np.argmin(x1**2) - r1 = np.hstack((r1[xi:],r1[:xi])) - tg = np.zeros(len(g)) - for i in range(len(x)): - tg[i] = np.add.reduce(g*r1)*(x[1]-x[0]) - r1 = np.hstack((r1[-1:],r1[:-1])) - return tg + xi = np.argmin(x1**2) + r1 = np.hstack((r1[xi:],r1[:xi])) + tg = np.zeros(len(g)) + for i in range(len(x)): + tg[i] = np.add.reduce(g*r1)*(x[1]-x[0]) + r1 = np.hstack((r1[-1:],r1[:-1])) + return tg def fitfunc(p,x,e): - A = p[0] - rv = p[1] - vrot = p[2] - s = p[3] - - g1 = np.exp(-0.5*((x-rv)/s)**2)/np.sqrt(2*np.pi*s*s) - d = x[1]-x[0] - x1 = np.arange(len(x))*d - x1 -= int(np.round(x1.mean())) - I = np.where(x1**2 < vrot**2)[0] - c1 = 2.*(1. - e) / (np.pi * vrot * (1. - e/3.)) - c2 = .5 * e / (vrot * (1.-e/3.)) - r1 = np.zeros(len(x1)) - r1[I] = (c1*np.sqrt(1.-(x1[I]/vrot)**2) + c2*(1. - (x1[I]/vrot)**2)) - - prof = conv(x,g1,x1,r1) - - ret = 1. - A*prof - return ret - + A = p[0] + rv = p[1] + vrot = p[2] + s = p[3] + + g1 = np.exp(-0.5*((x-rv)/s)**2)/np.sqrt(2*np.pi*s*s) + d = x[1]-x[0] + x1 = np.arange(len(x))*d + x1 -= int(np.round(x1.mean())) + I = np.where(x1**2 < vrot**2)[0] + c1 = 2.*(1. - e) / (np.pi * vrot * (1. - e/3.)) + c2 = .5 * e / (vrot * (1.-e/3.)) + r1 = np.zeros(len(x1)) + r1[I] = (c1*np.sqrt(1.-(x1[I]/vrot)**2) + c2*(1. - (x1[I]/vrot)**2)) + + prof = conv(x,g1,x1,r1) + + ret = 1. - A*prof + return ret + def errfunc(p, x, y, ldc): clutch = 0.0 mean = p[1] if (mean < np.min(x)): clutch = 1e10*(1.0 - np.exp(-np.abs(mean-np.min(x)) / 3) ) - if (mean > np.max(x)): + if (mean > np.max(x)): clutch = 1e10*(1.0 - np.exp(-np.abs(mean-np.max(x)) / 3) ) return np.ravel( (fitfunc(p,x,ldc) - y) ) + clutch p0 = np.array( [1.,vel0,vsini,1.] ) - + p1, success = scipy.optimize.leastsq(errfunc,p0, args=(X,Y,ldc)) #plt.plot(X,Y) #plt.plot(X, 1. - p1[0]*np.exp(-0.5*((X-p1[1])/p1[3])**2)/np.sqrt(2*np.pi*p1[3]*p1[3])) @@ -279,11 +280,11 @@ def errfunc(p, x, y, ldc): #plt.plot(X, 1. - p1[0]*r1) #plt.show() - + return p1, fitfunc(p1,X,ldc) def get_ldc(T,G,Z,M,ldfile = 'lin_coe_sloan2.dat'): - f = np.loadtxt(ldfile) - - I = np.argmin( np.sqrt((T - f[:,2])**2)/T + np.sqrt((G - f[:,1])**2)/G + np.sqrt((Z - f[:,3])**2)/np.sqrt(Z**2) ) - return f[I][5] \ No newline at end of file + f = np.loadtxt(ldfile) + + I = np.argmin( np.sqrt((T - f[:,2])**2)/T + np.sqrt((G - f[:,1])**2)/G + np.sqrt((Z - f[:,3])**2)/np.sqrt(Z**2) ) + return f[I][5] diff --git a/dupont/dupontpipe.py b/dupont/dupontpipe.py index 155d452..89e3f16 100644 --- a/dupont/dupontpipe.py +++ b/dupont/dupontpipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * ioff() @@ -91,18 +92,18 @@ reffile = dirin+'reffile.txt' if resolution != 40000 and resolution != 50000 and resolution != 60000: - raise ValueError("Input Resolution not permited. try 40000, 50000 or 60000\n") + raise ValueError("Input Resolution not permited. try 40000, 50000 or 60000\n") ####### GLOBAL VARIABLES ##### force_pre_process = False -force_bl = False # +force_bl = False # force_bkg = False force_P = False force_thar_extract = False -force_thar_wavcal = False +force_thar_wavcal = False force_sci_extract = False -force_sci_proc = True # -force_RV = False # +force_sci_proc = True # +force_RV = False # force_stellar_pars = False force_corr = False @@ -126,23 +127,23 @@ min_extract_col = 50 max_extract_col = 2000 -#npar_wsol = 27 #number of parameters of wavelength solution +#npar_wsol = 27 #number of parameters of wavelength solution ncoef_x = 5 ncoef_m = 6 npar_wsol = (min(ncoef_x,ncoef_m) + 1) * (2*max(ncoef_x,ncoef_m) - min(ncoef_x,ncoef_m) + 2) / 2 n_last = 55 # last echelle order as defined in the reference wavelength files -ntotal = 64 +ntotal = 64 oro0 = 36 order_dir = base+'dupont/wavcals/' models_path = base+'data/COELHO_MODELS/R_40000b/' -print "\n\n\tEchelle Du Pont 2.5m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tEchelle Du Pont 2.5m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # file containing the log log = dirout+'night.log' @@ -154,21 +155,21 @@ f = open(log,'r') lines = f.readlines() -print '\tThese are all the images to proccess:' +print('\tThese are all the images to proccess:') for bias in biases: - hd = pyfits.getheader(bias) - print '\tbias', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],bias -print '\n' + hd = pyfits.getheader(bias) + print('\tbias', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],bias) +print('\n') for dark in darks: - hd = pyfits.getheader(dark) - print '\tdark', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],dark -print '\n' + hd = pyfits.getheader(dark) + print('\tdark', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],dark) +print('\n') for milky in milkyflats: - hd = pyfits.getheader(milky) - print '\tmilky', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],milky -print '\n' + hd = pyfits.getheader(milky) + print('\tmilky', hd['EXPTYPE'], hd['EXPTIME'], hd['UT-DATE'],hd['UT-TIME'],milky) +print('\n') for line in lines: - print '\t'+line[:-1] + print('\t'+line[:-1]) if stst == 'last': if os.access(dirout+'findstar.txt',os.F_OK): @@ -183,43 +184,43 @@ fst.close() if stblaze == 'same': - print '\n\tThe pipeline will use the image that traces the orders to derive the blaze function ...' + print('\n\tThe pipeline will use the image that traces the orders to derive the blaze function ...') stblaze = stst if ( (os.access(dirout+'Flat.fits',os.F_OK) == False) or\ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): - print "\n\tGenerating Master calibration frames..." + print("\n\tGenerating Master calibration frames...") MasterBias, RO_bias, GA_bias = dupontutils.MedianCombine(biases, zero_bo=False, dark_bo=False, flat_bo=False) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") MDARKS = [] dark_times = [] if dark_substraction: - + for dark in darks: - hd = pyfits.getheader(dark) - if len(dark_times) == 0: - dark_times.append(hd['EXPTIME']) - else: - if dark_times.count(hd['EXPTIME']) == 0: - dark_times.append(hd['EXPTIME']) + hd = pyfits.getheader(dark) + if len(dark_times) == 0: + dark_times.append(hd['EXPTIME']) + else: + if dark_times.count(hd['EXPTIME']) == 0: + dark_times.append(hd['EXPTIME']) dark_groups = [] - ndark_times = [] + ndark_times = [] for time in dark_times: group = [] for dark in darks: @@ -241,7 +242,7 @@ hdu.writeto( dirout+'DARK_'+str(int(dark_times[i]))+'s.fits' ) MDARKS.append(dirout+'DARK_'+str(int(dark_times[i]))+'s.fits') i+=1 - print "\t\t-> Masterdarks: done!" + print("\t\t-> Masterdarks: done!") force_flat_comb = True if force_flat_comb or os.access(dirout+'Flat.fits',os.F_OK) == False: @@ -266,13 +267,13 @@ Flat = h[0].data RON = h[0].header['RON'] GAIN = h[0].header['GAIN'] - print "\t\t-> Masterflat: done!" - + print("\t\t-> Masterflat: done!") + ftra = True median_filter = False # Find orders & traces - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") if ftra or os.access(dirout+'trace.pkl',os.F_OK) == False: h = pyfits.open(dirin+stst)[0] hth = pyfits.getheader(dirin+stst) @@ -282,9 +283,9 @@ d /= Flat if bad_colummn: - d = dupontutils.b_col(d) + d = dupontutils.b_col(d) - d = d[:1420,:] + d = d[:1420,:] if use_ref: c_comp = pickle.load( open( ref_traces, 'r' ) )['c_all'] nord = pickle.load( open( ref_traces, 'r' ) )['nord'] @@ -292,14 +293,14 @@ c_all,pshift = GLOBALutils.retrace( d, c_comp ) else: c_all, nord = GLOBALutils.get_them(d,5,trace_degree,mode=1) - print '\t\t'+str(nord)+' orders found...' + print('\t\t'+str(nord)+' orders found...') else: trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) c_all = trace_dict['c_all'] nord = trace_dict['nord'] GAIN = trace_dict['GA_ob'] RON = trace_dict['RO_ob'] - + trace_dict = {'c_all':c_all, 'nord':nord, 'GA_ob': GAIN, 'RO_ob': RON, 'DARKS':MDARKS, 'dtimes':dark_times} pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) @@ -322,7 +323,7 @@ h = pyfits.open(dirout+'Flat.fits') Flat = h[0].data -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') for fsim in ThAr_ref: hth = pyfits.getheader(fsim) @@ -335,8 +336,8 @@ thar_fits_simple = dirout+'ThAr_'+hth['DATE-OBS']+'_'+hth['UT-TIME'][:2]+ \ '-'+hth['UT-TIME'][3:5]+'-'+hth['UT-TIME'][6:]+'.spec.simple.fits' - if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") close_spec = dupontutils.get_close(thmjd,hth['RA-D'],hth['DEC-D'],objects) temp_spec = pyfits.getdata(close_spec) temp_hd = pyfits.getheader(close_spec) @@ -354,9 +355,9 @@ hdu = pyfits.PrimaryHDU( thar_Ss ) hdu.writeto( thar_fits_simple ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") # compute wavelength calibration files thtimes = [] @@ -380,13 +381,13 @@ thar_Ss = pyfits.getdata(thar_fits_simple) if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print " \t\tWorking on ThAr file", thar - + print(" \t\tWorking on ThAr file", thar) + RON = hth['ENOISE'] GAIN = hth['EGAIN'] - + lines_thar = thar_Ss[:,:] - if os.access(dirout+'id_orders.pkl',os.F_OK) == False or force_corr: + if os.access(dirout+'id_orders.pkl',os.F_OK) == False or force_corr: maxes = 0 or32 = 0 for order in range(len(lines_thar)): @@ -396,7 +397,7 @@ rough_shift = shift or32 = order or0 = or32 - 32 - + if or0 >= 0: orwa = 0 else: @@ -413,11 +414,11 @@ if os.access(dirout+'id_orders.pkl',os.F_OK): os.remove(dirout+'id_orders.pkl') - + pdict = {'or0':or0, 'orwa':orwa, 'n_last':n_last, 'rough_shift':rough_shift,'nn':nn} pickle.dump( pdict, open(dirout+'id_orders.pkl', 'w' ) ) ntr += 1 - + else: pdict = pickle.load(open(dirout+'id_orders.pkl', 'r')) or0 = pdict['or0'] @@ -425,7 +426,7 @@ n_last = pdict['n_last'] nn = pdict['nn'] rough_shift = pdict['rough_shift'] - + iv_thar = 1/((lines_thar/GAIN) + (RON**2/GAIN**2)) All_Pixel_Centers = np.array([]) @@ -442,7 +443,7 @@ OK = [] OW = [] - + while order < n_last: order_s = str(order) if (order < 10): @@ -456,21 +457,21 @@ coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms,\ residuals, centroids, sigmas, intensities = GLOBALutils.Initial_Wav_Calibration( \ order_dir+'order_'+order_s+'o.iwdat', thar_order, order, wei, rmsmax=1000, \ - minlines=10, FixEnds=False, Dump_Argon=False, Dump_AllLines=True, Cheby=use_cheby, \ + minlines=10, FixEnds=False, Dump_Argon=False, Dump_AllLines=True, Cheby=use_cheby, \ rough_shift = rough_shift) fwhms_lns = sigmas*2.355 inis_lns = pixel_centers - fwhms_lns*0.5 - fins_lns = pixel_centers + fwhms_lns*0.5 + fins_lns = pixel_centers + fwhms_lns*0.5 inis_wvs = GLOBALutils.Cheby_eval(coeffs_pix2wav,inis_lns,float(len(thar_order))) fins_wvs = GLOBALutils.Cheby_eval(coeffs_pix2wav,fins_lns,float(len(thar_order))) fwhms_wvs = inis_wvs - fins_wvs resolution2 = wavelengths / fwhms_wvs if wavelengths.max() > 5500 and wavelengths.min()<5500: - print "\t\t\tmedian Resolution of order", order, '=', np.around(np.median(resolution2)) - - if (order == 32): + print("\t\t\tmedian Resolution of order", order, '=', np.around(np.median(resolution2))) + + if (order == 32): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) else: @@ -484,7 +485,7 @@ All_Intensities = np.append( All_Intensities, intensities ) All_residuals = np.append( All_Residuals, residuals) All_Sigmas = np.append( All_Sigmas,sigmas) - + order += 1 orre += 1 @@ -492,7 +493,7 @@ p0[0] = (32+oro0) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders, np.ones(All_Intensities.shape), p0, npix=len(lines_thar[orre,:]), Cheby=use_cheby, maxrms=150, Inv=Inverse_m, minlines=800,order0=oro0,nx=ncoef_x,nm=ncoef_m,ntotal=ntotal) + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders, np.ones(All_Intensities.shape), p0, npix=len(lines_thar[orre,:]), Cheby=use_cheby, maxrms=150, Inv=Inverse_m, minlines=800,order0=oro0,nx=ncoef_x,nm=ncoef_m,ntotal=ntotal) if rms_ms/np.sqrt(float(len(G_pix))) < 20: pdict = {'p1':p1, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II,\ @@ -507,7 +508,7 @@ else: bad_thars.append(counter) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict_temp = pickle.load(open(wavsol_pkl, 'r')) if pdict_temp['rms_ms']/np.sqrt(float(len(pdict_temp['G_pix']))) < 20: thtimes.append(thmjd) @@ -546,43 +547,43 @@ dat -= MasterBias if dark_substraction and hd['EXPTIME'] > 600: - dat -= utils.get_dark(MDARKS, hd['EXPTIME']) + dat -= utils.get_dark(MDARKS, hd['EXPTIME']) if bad_colummn: - dat = utils.b_col(dat) + dat = utils.b_col(dat) dat = dat/Flat - + print 'Recentering traces...' c_alls,pshift = GLOBALutils.retrace( dat, c_all ) Centers = np.zeros((len(c_alls),dat.shape[1])) for i in range(nord): - Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) + Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) print 'Scatter Light Determination...' if np.median(dat[Centers[0,1024]:Centers[-1,1024],1024])> 0: - if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): - #bkg = bkg_simple.get_scat(dat,Centers) - bkg = GLOBALutils.get_scat(dat,Centers,span=6) - if (os.access(bkg_obj_fits,os.F_OK)): - os.remove( bkg_obj_fits ) - hdu = pyfits.PrimaryHDU( bkg ) - hdu.writeto( bkg_obj_fits ) - else: - bkg = pyfits.getdata(bkg_obj_fits) - dat -= bkg + if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): + #bkg = bkg_simple.get_scat(dat,Centers) + bkg = GLOBALutils.get_scat(dat,Centers,span=6) + if (os.access(bkg_obj_fits,os.F_OK)): + os.remove( bkg_obj_fits ) + hdu = pyfits.PrimaryHDU( bkg ) + hdu.writeto( bkg_obj_fits ) + else: + bkg = pyfits.getdata(bkg_obj_fits) + dat -= bkg print 'P matrix determination...' force_P = False if os.access(P_fits,os.F_OK) == False or force_P: - P = GLOBALutils.obtain_P(dat,c_alls,ext_aperture,RON,GAIN,NSigma_Marsh, S_Marsh,N_Marsh, Marsh_alg,0,2047,npools) - if (os.access(P_fits,os.F_OK)): - os.remove( P_fits ) - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( P_fits ) + P = GLOBALutils.obtain_P(dat,c_alls,ext_aperture,RON,GAIN,NSigma_Marsh, S_Marsh,N_Marsh, Marsh_alg,0,2047,npools) + if (os.access(P_fits,os.F_OK)): + os.remove( P_fits ) + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( P_fits ) else: - P = pyfits.getdata(P_fits) + P = pyfits.getdata(P_fits) """ -print '\n\tProcessing of science images:' +print('\n\tProcessing of science images:') sthd = pyfits.getheader(dirin+stblaze) stname = sthd['OBJECT'] @@ -608,124 +609,124 @@ for obj in objects: - print '\n' - print "\t--> Working on image: ", obj - - hd = pyfits.getheader(obj) - nombre = hd['OBJECT'] - RON, GAIN = hd['ENOISE'], hd['EGAIN'] - - print "\t\tObject name:",nombre - - nama = nombre + '_' + hd['DATE-OBS'] + '_' + hd['UT-TIME'][:2] +\ - '-' + hd['UT-TIME'][3:5]+'-' + hd['UT-TIME'][6:] - - obj_fits = dirout + nama + '.spec.fits.S' - obj_fits_simple = dirout + nama + '.spec.simple.fits.S' - bkg_obj_fits = dirout + 'Bkg_' + nama + '.fits' - P_fits = dirout + 'P_' + nama + '.fits' - - only_simple = False - if stname == nombre: - stpath = obj_fits_simple - - cond2 = False - if only_simple: - if ( os.access(obj_fits_simple,os.F_OK) == False ) or (force_sci_extract) : - cond2 = True - else: - if ( os.access(obj_fits,os.F_OK) == False ) or ( os.access(obj_fits_simple,os.F_OK) == False ) or (force_sci_extract) or ( os.access(P_fits,os.F_OK) == False ): - cond2 = True - - if cond2: - - print "\t\tNo previous extraction or extraction forced for science file", obj, "extracting..." - - dat = pyfits.getdata(obj) - hdt = pyfits.getheader(obj) - dat = dupontutils.OverscanTrim(dat,hdt['BIASSEC']) - dat -= MasterBias - if dark_substraction and hd['EXPTIME'] >= 899: - dat -= dupontutils.get_dark(MDARKS, hd['EXPTIME']) - - if bad_colummn: - dat = dupontutils.b_col(dat) - dat = dat/Flat - - c_alls,pshift = GLOBALutils.retrace( dat, c_all ) - - Centers = np.zeros((len(c_alls),dat.shape[1])) - for i in range(nord): - Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) - - #print 'Scatter Light Determination...' - - #print 'SN',np.median(dat[Centers[0,1024]:Centers[-1,1024],1024]) - if np.median(dat[Centers[0,1024]:Centers[-1,1024],1024])> 0: - if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): - bkg = GLOBALutils.get_scat(dat,Centers,span=6) - if (os.access(bkg_obj_fits,os.F_OK)): - os.remove( bkg_obj_fits ) - hdu = pyfits.PrimaryHDU( bkg ) - hdu.writeto( bkg_obj_fits ) - else: - bkg = pyfits.getdata(bkg_obj_fits) - dat -= bkg - else: - NCosmic_Marsh = 6 - - #print 'P matrix determination...' - use_ref_P = False - if use_ref_P: - P = pyfits.getdata(dirout + 'P_ref.fits') - Pn = P.copy() - for i in range(P.shape[0]): - j = i - pshift - if j < 0 or j>=P.shape[0]: - Pn[i] = 0. - else: - Pn[i] = P[j] - P = Pn.copy() - - else: - if os.access(P_fits,os.F_OK) == False or force_P: - P = GLOBALutils.obtain_P(dat,c_alls,ext_aperture,RON,\ - GAIN,NSigma_Marsh, S_Marsh,N_Marsh, Marsh_alg,0,2047,npools) - if (os.access(P_fits,os.F_OK)): - os.remove( P_fits ) - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( P_fits ) - else: - P = pyfits.getdata(P_fits) - - if only_simple==False: - obj_S = GLOBALutils.optimal_extraction(dat,P,c_alls,ext_aperture,RON,\ - GAIN,S_Marsh,NCosmic_Marsh,50,2000,npools) - obj_S = obj_S[::-1] - if (os.access(obj_fits,os.F_OK)): - os.remove( obj_fits ) - hdu = pyfits.PrimaryHDU( obj_S ) - hdu.writeto( obj_fits ) - - obj_Ss = GLOBALutils.simple_extraction(dat,c_alls,ext_aperture,min_extract_col,max_extract_col,npools) - obj_Ss = obj_Ss[::-1] - - if (os.access(obj_fits_simple,os.F_OK)): - os.remove( obj_fits_simple ) - - hdu = pyfits.PrimaryHDU( obj_Ss ) - hdu.writeto( obj_fits_simple ) + print('\n') + print("\t--> Working on image: ", obj) + + hd = pyfits.getheader(obj) + nombre = hd['OBJECT'] + RON, GAIN = hd['ENOISE'], hd['EGAIN'] + + print("\t\tObject name:",nombre) + + nama = nombre + '_' + hd['DATE-OBS'] + '_' + hd['UT-TIME'][:2] +\ + '-' + hd['UT-TIME'][3:5]+'-' + hd['UT-TIME'][6:] + + obj_fits = dirout + nama + '.spec.fits.S' + obj_fits_simple = dirout + nama + '.spec.simple.fits.S' + bkg_obj_fits = dirout + 'Bkg_' + nama + '.fits' + P_fits = dirout + 'P_' + nama + '.fits' + + only_simple = False + if stname == nombre: + stpath = obj_fits_simple + + cond2 = False + if only_simple: + if ( os.access(obj_fits_simple,os.F_OK) == False ) or (force_sci_extract) : + cond2 = True + else: + if ( os.access(obj_fits,os.F_OK) == False ) or ( os.access(obj_fits_simple,os.F_OK) == False ) or (force_sci_extract) or ( os.access(P_fits,os.F_OK) == False ): + cond2 = True + + if cond2: + + print("\t\tNo previous extraction or extraction forced for science file", obj, "extracting...") + + dat = pyfits.getdata(obj) + hdt = pyfits.getheader(obj) + dat = dupontutils.OverscanTrim(dat,hdt['BIASSEC']) + dat -= MasterBias + if dark_substraction and hd['EXPTIME'] >= 899: + dat -= dupontutils.get_dark(MDARKS, hd['EXPTIME']) + + if bad_colummn: + dat = dupontutils.b_col(dat) + dat = dat/Flat + + c_alls,pshift = GLOBALutils.retrace( dat, c_all ) + + Centers = np.zeros((len(c_alls),dat.shape[1])) + for i in range(nord): + Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) + + #print 'Scatter Light Determination...' + + #print 'SN',np.median(dat[Centers[0,1024]:Centers[-1,1024],1024]) + if np.median(dat[Centers[0,1024]:Centers[-1,1024],1024])> 0: + if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): + bkg = GLOBALutils.get_scat(dat,Centers,span=6) + if (os.access(bkg_obj_fits,os.F_OK)): + os.remove( bkg_obj_fits ) + hdu = pyfits.PrimaryHDU( bkg ) + hdu.writeto( bkg_obj_fits ) + else: + bkg = pyfits.getdata(bkg_obj_fits) + dat -= bkg + else: + NCosmic_Marsh = 6 + + #print 'P matrix determination...' + use_ref_P = False + if use_ref_P: + P = pyfits.getdata(dirout + 'P_ref.fits') + Pn = P.copy() + for i in range(P.shape[0]): + j = i - pshift + if j < 0 or j>=P.shape[0]: + Pn[i] = 0. + else: + Pn[i] = P[j] + P = Pn.copy() + + else: + if os.access(P_fits,os.F_OK) == False or force_P: + P = GLOBALutils.obtain_P(dat,c_alls,ext_aperture,RON,\ + GAIN,NSigma_Marsh, S_Marsh,N_Marsh, Marsh_alg,0,2047,npools) + if (os.access(P_fits,os.F_OK)): + os.remove( P_fits ) + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( P_fits ) + else: + P = pyfits.getdata(P_fits) + + if only_simple==False: + obj_S = GLOBALutils.optimal_extraction(dat,P,c_alls,ext_aperture,RON,\ + GAIN,S_Marsh,NCosmic_Marsh,50,2000,npools) + obj_S = obj_S[::-1] + if (os.access(obj_fits,os.F_OK)): + os.remove( obj_fits ) + hdu = pyfits.PrimaryHDU( obj_S ) + hdu.writeto( obj_fits ) + + obj_Ss = GLOBALutils.simple_extraction(dat,c_alls,ext_aperture,min_extract_col,max_extract_col,npools) + obj_Ss = obj_Ss[::-1] + + if (os.access(obj_fits_simple,os.F_OK)): + os.remove( obj_fits_simple ) + + hdu = pyfits.PrimaryHDU( obj_Ss ) + hdu.writeto( obj_fits_simple ) ################################################################################################################### ############################################### get blaze function ################################################ ################################################################################################################### -print '\n\tComputing the blaze function using the standard star...' +print('\n\tComputing the blaze function using the standard star...') -if os.access(dirout+'blaze_f.fits',os.F_OK) == False or force_bl: +if os.access(dirout+'blaze_f.fits',os.F_OK) == False or force_bl: index = -1 hd = pyfits.getheader(dirin+stblaze) nombre = hd['OBJECT'] - exptime = hd['EXPTIME'] + exptime = hd['EXPTIME'] RA = hd['RA-D'] DEC = hd['DEC-D'] RON = hd['ENOISE'] @@ -747,12 +748,12 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - res = jplephem.pulse_delay(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) + res = jplephem.pulse_delay(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) mbjd = scmjd + res['delay'][0] / (3600.0 * 24.0) gobs = ephem.Observer() gobs.name = 'DUPONT' - gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.lat = rad(latitude) # lat/long in decimal degrees gobs.long = rad(longitude) gobs.date = hd['UT-DATE'].replace('-','/') + ' ' + hd['UT-TIME'] @@ -764,7 +765,7 @@ res = jplephem.object_doppler("Moon", int(scmjd), scmjd%1, 1, 0.0) lunation,moon_state,moonsep2,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,RA,DEC) refvel = bcvel_baryc + moonvel - print '\t\tBarycentric velocity:',refvel + print('\t\tBarycentric velocity:',refvel) i=0 mind = 1000.0 @@ -784,7 +785,7 @@ oro0 = pdict1['oro0'] final = np.zeros( [3, n_last-orwa1, np.shape(obj_S)[1]] ) - equis = np.arange( np.shape(obj_S)[1] ) + equis = np.arange( np.shape(obj_S)[1] ) order = orwa1 orre = or01 final2 = np.zeros( [nn, np.shape(obj_S)[1]] ) @@ -798,11 +799,11 @@ orre += 1 final[2,:,:] = dupontutils.get_blaze(final[0,:,:],final[1,:,:]) - + blaze = np.zeros((2,final.shape[1],final.shape[2])) blaze[0] = final[0] blaze[1] = dupontutils.get_blaze(final[0,:,:],final[1,:,:]) - + if (os.access(dirout+'blaze_f.fits',os.F_OK)): os.remove( dirout+'blaze_f.fits' ) hdu = pyfits.PrimaryHDU( blaze ) @@ -818,7 +819,7 @@ ############################################ Final output ###################################################### ################################################################################################################# -print "\n\tBuilding the final output spectra..." +print("\n\tBuilding the final output spectra...") for obj in objects: hd = pyfits.getheader(obj) @@ -826,195 +827,195 @@ nama = nombre+'_'+hd['DATE-OBS']+'_'+hd['UT-TIME'][:2]+'-'+hd['UT-TIME'][3:5]+'-'+hd['UT-TIME'][6:] nf = nama+'_final.fits' nfS = nama+'_finalS.fits' - print "\n\t\t-->Building", nf + print("\n\t\t-->Building", nf) if (os.access(dirout+'proc/'+nf,os.F_OK)) == False or force_sci_proc: - index1,index2 = -1,-1 - - # Get observing info from header - hd = pyfits.getheader(obj) - nombre = hd['OBJECT'] - exptime = hd['EXPTIME'] - RA = hd['RA-D'] - DEC = hd['DEC-D'] - RON = hd['ENOISE'] - GAIN = hd['EGAIN'] - altitude = hd['SITEALT'] - latitude = hd['SITELAT'] - longitude = hd['SITELONG'] - epoch = hd['EPOCH'] - - scmjd,scmjd0 = dupontutils.mjd_fromheader(hd) - ra2,dec2 = GLOBALutils.getcoords(obname,scmjd,filen=reffile) - if ra2 !=0 and dec2 != 0: - RA = ra2 - DEC = dec2 - else: - print '\t\tUsing the coordinates found in the image header.' - - # set info for compute the baricentric correction - iers = GLOBALutils.JPLiers( baryc_dir, scmjd-999.0, scmjd+999.0 ) - obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) - obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) - jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) - jplephem.set_observer_coordinates( obpos[0], obpos[1], obpos[2] ) - res = jplephem.doppler_fraction(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) - lbary_ltopo = 1.0 + res['frac'][0] - bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 #This in the barycentric velocity - res = jplephem.pulse_delay(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) - scmbjd = scmjd + res['delay'][0] / (3600.0 * 24.0) #This is the modified barycentric julian day of the observation - - # set observatory info to retrive info about the moon - gobs = ephem.Observer() - gobs.name = 'DUPONT' - gobs.lat = rad(latitude) - gobs.long = rad(longitude) - #gobs.date = hd['UT-DATE'] + ' ' + hd['UT-TIME'].replace(':','_') - gobs.date = hd['UT-DATE'].replace('-','/') + ' ' + hd['UT-TIME'] - - mephem = ephem.Moon() - mephem.compute(gobs) - Mcoo = jplephem.object_track("Moon", int(scmjd), float(scmjd%1), 1, 0.0) - Mp = jplephem.barycentric_object_track("Moon", int(scmjd), float(scmjd%1), 1, 0.0) - Sp = jplephem.barycentric_object_track("Sun", int(scmjd), float(scmjd%1), 1, 0.0) - res = jplephem.object_doppler("Moon", int(scmjd), scmjd%1, 1, 0.0) - lunation,moon_state,moonsep2,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,RA,DEC) - refvel = bcvel_baryc + moonvel #This is the velocity of the spectrum of the moon with the applied barycentric correction in the direction of the target. - - print '\t\t\tBarycentric velocity:',refvel - - # Set the ThAr lamps for applying the wavelength solution - if scmjd < thtimes[0]: - "\t\t\tProblem with ThAr and science times" - index1 = 0 - index2 = 0 - elif scmjd > thtimes[-1]: - "\t\t\tProblem with ThAr and science times" - index1 = -1 - index2 = -1 - else: - "\t\t\tThAr images taken before and after science image found..." - for i in range(len(thtimes)-1): - if scmjd >= thtimes[i] and scmjd < thtimes[i+1]: - index1 = i - index2 = i+1 - break - - if abs(RA-thRA[index1]) > 0.005 or abs(DEC-thDEC[index1]) > 0.005: - index1 = index2 - if abs(RA-thRA[index2]) > 0.005 or abs(DEC-thDEC[index2]) > 0.005: - index2 = index1 - - obj_fits = dirout+nama+'.spec.fits.S' - obj_fits_simple = dirout+nama+'.spec.simple.fits.S' - obj_S = pyfits.getdata(obj_fits) - obj_Ss = pyfits.getdata(obj_fits_simple) - - wavsol_pkl1 = thnames[index1] - pdict1 = pickle.load(open(wavsol_pkl1,'r')) - global1 = pdict1['p1'] - or01 = pdict1['or0'] - orwa1 = pdict1['orwa'] - oro0 = pdict1['oro0'] - wavsol_pkl2 = thnames[index2] - pdict2 = pickle.load(open(wavsol_pkl2,'r')) - global2 = pdict2['p1'] - - final = np.zeros( [11, nn, np.shape(obj_S)[2]] ) - finalS = np.zeros( [2, nn, np.shape(obj_Ss)[1]] ) - equis = np.arange( np.shape(obj_S)[2] ) - order = orwa1 - orre = or01 - - while order < n_last: - - m = order + oro0 - chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0, ntotal=ntotal, npix=obj_S.shape[2], Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo*(1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global1,chebs,ncoef_x,ncoef_m) - WavSol2 = lbary_ltopo*(1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global2,chebs,ncoef_x,ncoef_m) - - final[0,orre,:] = GLOBALutils.ToVacuum(0.5*(WavSol[::-1]+WavSol2[::-1])) - final[1,orre,:] = obj_S[orre,1,:][::-1] - final[2,orre,:] = obj_S[orre,2,:][::-1] - tck = scipy.interpolate.splrep(blaze[0,orre,:],blaze[1,orre,:],k=3,s=0) - blin = scipy.interpolate.splev(final[0,orre,:],tck,der = 0) - final[3,orre,:] = final[1,orre,:]/blin - final[4,orre,:] = final[2,orre,:]*(blin**2) - - X = final[0,orre,:] - Z = final[3,orre,:] - Z[:150] = 0.0 - Z[-150:] = 0.0 - - I = np.isnan(Z) - Z[I] = 0.0 - I = np.where(Z!=0)[0] - if len(I)>0: - cp = continuum.NORM_single(X,Z,orden=1) - ratio = np.polyval(cp,X) - else: - ratio = np.ones(len(X)) - - ratio = np.polyval(cp,X) - final[5,orre,:] = final[3,orre,:]/ratio - Inan = np.where( np.isnan(final[1,orre,:]) == True )[0] - final[5,orre,Inan] = 1. - final[6,orre,:] = final[4,orre,:]*(ratio**2) - final[7,orre,:] = ratio - final[8,orre,:] = ratio*blin / np.sqrt( ratio *blin / GAIN + (RON/GAIN)**2 ) - - spl = scipy.interpolate.splrep(np.arange(len(final[0,orre,:])),final[0,orre,:] ,k=3) - dlambda_dx = scipy.interpolate.splev(np.arange(len(final[0,orre,:])), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - - LL = np.where( final[5,orre,:] != 0 )[0] - medflx = scipy.signal.medfilt( final[5,orre,LL], 3 ) - fres = final[5,orre,LL] - medflx - fdev = np.sqrt(np.var(fres)) - II = np.where(final[5,orre,:] > 1. + 5*fdev)[0] - final[5,orre,II] = 1. - - final[9,orre,:] = final[5,orre,:] * (dlambda_dx ** 1) - final[10,orre,:] = final[6,orre,:] / (dlambda_dx ** 2) - - finalS[0,orre,:] = WavSol[::-1] - finalS[1,orre,:] = obj_Ss[orre,:][::-1] - - - order += 1 - orre += 1 - - hdu = pyfits.PrimaryHDU( final ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD',scmjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD',scmbjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE',hd['DATE-OBS']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT',hd['UT-TIME']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hd['EXPTIME']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)',bcvel_baryc,'[km/s]') - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)',lbary_ltopo) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME',nombre) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',hd['RA']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',hd['DEC']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA-D',hd['RA-D']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC-D',hd['DEC-D']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',RA) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',DEC) - hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',hd['EQUINOX']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',hd['SITELAT']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',hd['SITELONG']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',hd['SITEALT']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',hd['AIRMASS']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON_VEL',refvel,'[km/s]') - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONST',moon_state) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LUNATION',lunation) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONSEP',moonsep2) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONALT',float(mephem.alt)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SMOONALT',str(mephem.alt)) - - if (os.access(dirout+'proc/'+nf,os.F_OK)): - os.remove( dirout+'proc/'+nf ) - hdu.writeto( dirout+'proc/'+nf ) - + index1,index2 = -1,-1 + + # Get observing info from header + hd = pyfits.getheader(obj) + nombre = hd['OBJECT'] + exptime = hd['EXPTIME'] + RA = hd['RA-D'] + DEC = hd['DEC-D'] + RON = hd['ENOISE'] + GAIN = hd['EGAIN'] + altitude = hd['SITEALT'] + latitude = hd['SITELAT'] + longitude = hd['SITELONG'] + epoch = hd['EPOCH'] + + scmjd,scmjd0 = dupontutils.mjd_fromheader(hd) + ra2,dec2 = GLOBALutils.getcoords(obname,scmjd,filen=reffile) + if ra2 !=0 and dec2 != 0: + RA = ra2 + DEC = dec2 + else: + print('\t\tUsing the coordinates found in the image header.') + + # set info for compute the baricentric correction + iers = GLOBALutils.JPLiers( baryc_dir, scmjd-999.0, scmjd+999.0 ) + obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) + obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) + jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) + jplephem.set_observer_coordinates( obpos[0], obpos[1], obpos[2] ) + res = jplephem.doppler_fraction(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) + lbary_ltopo = 1.0 + res['frac'][0] + bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 #This in the barycentric velocity + res = jplephem.pulse_delay(RA/15.0, DEC, int(scmjd), scmjd%1, 1, 0.0) + scmbjd = scmjd + res['delay'][0] / (3600.0 * 24.0) #This is the modified barycentric julian day of the observation + + # set observatory info to retrive info about the moon + gobs = ephem.Observer() + gobs.name = 'DUPONT' + gobs.lat = rad(latitude) + gobs.long = rad(longitude) + #gobs.date = hd['UT-DATE'] + ' ' + hd['UT-TIME'].replace(':','_') + gobs.date = hd['UT-DATE'].replace('-','/') + ' ' + hd['UT-TIME'] + + mephem = ephem.Moon() + mephem.compute(gobs) + Mcoo = jplephem.object_track("Moon", int(scmjd), float(scmjd%1), 1, 0.0) + Mp = jplephem.barycentric_object_track("Moon", int(scmjd), float(scmjd%1), 1, 0.0) + Sp = jplephem.barycentric_object_track("Sun", int(scmjd), float(scmjd%1), 1, 0.0) + res = jplephem.object_doppler("Moon", int(scmjd), scmjd%1, 1, 0.0) + lunation,moon_state,moonsep2,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,RA,DEC) + refvel = bcvel_baryc + moonvel #This is the velocity of the spectrum of the moon with the applied barycentric correction in the direction of the target. + + print('\t\t\tBarycentric velocity:',refvel) + + # Set the ThAr lamps for applying the wavelength solution + if scmjd < thtimes[0]: + "\t\t\tProblem with ThAr and science times" + index1 = 0 + index2 = 0 + elif scmjd > thtimes[-1]: + "\t\t\tProblem with ThAr and science times" + index1 = -1 + index2 = -1 + else: + "\t\t\tThAr images taken before and after science image found..." + for i in range(len(thtimes)-1): + if scmjd >= thtimes[i] and scmjd < thtimes[i+1]: + index1 = i + index2 = i+1 + break + + if abs(RA-thRA[index1]) > 0.005 or abs(DEC-thDEC[index1]) > 0.005: + index1 = index2 + if abs(RA-thRA[index2]) > 0.005 or abs(DEC-thDEC[index2]) > 0.005: + index2 = index1 + + obj_fits = dirout+nama+'.spec.fits.S' + obj_fits_simple = dirout+nama+'.spec.simple.fits.S' + obj_S = pyfits.getdata(obj_fits) + obj_Ss = pyfits.getdata(obj_fits_simple) + + wavsol_pkl1 = thnames[index1] + pdict1 = pickle.load(open(wavsol_pkl1,'r')) + global1 = pdict1['p1'] + or01 = pdict1['or0'] + orwa1 = pdict1['orwa'] + oro0 = pdict1['oro0'] + wavsol_pkl2 = thnames[index2] + pdict2 = pickle.load(open(wavsol_pkl2,'r')) + global2 = pdict2['p1'] + + final = np.zeros( [11, nn, np.shape(obj_S)[2]] ) + finalS = np.zeros( [2, nn, np.shape(obj_Ss)[1]] ) + equis = np.arange( np.shape(obj_S)[2] ) + order = orwa1 + orre = or01 + + while order < n_last: + + m = order + oro0 + chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0, ntotal=ntotal, npix=obj_S.shape[2], Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo*(1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global1,chebs,ncoef_x,ncoef_m) + WavSol2 = lbary_ltopo*(1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global2,chebs,ncoef_x,ncoef_m) + + final[0,orre,:] = GLOBALutils.ToVacuum(0.5*(WavSol[::-1]+WavSol2[::-1])) + final[1,orre,:] = obj_S[orre,1,:][::-1] + final[2,orre,:] = obj_S[orre,2,:][::-1] + tck = scipy.interpolate.splrep(blaze[0,orre,:],blaze[1,orre,:],k=3,s=0) + blin = scipy.interpolate.splev(final[0,orre,:],tck,der = 0) + final[3,orre,:] = final[1,orre,:]/blin + final[4,orre,:] = final[2,orre,:]*(blin**2) + + X = final[0,orre,:] + Z = final[3,orre,:] + Z[:150] = 0.0 + Z[-150:] = 0.0 + + I = np.isnan(Z) + Z[I] = 0.0 + I = np.where(Z!=0)[0] + if len(I)>0: + cp = continuum.NORM_single(X,Z,orden=1) + ratio = np.polyval(cp,X) + else: + ratio = np.ones(len(X)) + + ratio = np.polyval(cp,X) + final[5,orre,:] = final[3,orre,:]/ratio + Inan = np.where( np.isnan(final[1,orre,:]) == True )[0] + final[5,orre,Inan] = 1. + final[6,orre,:] = final[4,orre,:]*(ratio**2) + final[7,orre,:] = ratio + final[8,orre,:] = ratio*blin / np.sqrt( ratio *blin / GAIN + (RON/GAIN)**2 ) + + spl = scipy.interpolate.splrep(np.arange(len(final[0,orre,:])),final[0,orre,:] ,k=3) + dlambda_dx = scipy.interpolate.splev(np.arange(len(final[0,orre,:])), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + + LL = np.where( final[5,orre,:] != 0 )[0] + medflx = scipy.signal.medfilt( final[5,orre,LL], 3 ) + fres = final[5,orre,LL] - medflx + fdev = np.sqrt(np.var(fres)) + II = np.where(final[5,orre,:] > 1. + 5*fdev)[0] + final[5,orre,II] = 1. + + final[9,orre,:] = final[5,orre,:] * (dlambda_dx ** 1) + final[10,orre,:] = final[6,orre,:] / (dlambda_dx ** 2) + + finalS[0,orre,:] = WavSol[::-1] + finalS[1,orre,:] = obj_Ss[orre,:][::-1] + + + order += 1 + orre += 1 + + hdu = pyfits.PrimaryHDU( final ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD',scmjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD',scmbjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE',hd['DATE-OBS']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT',hd['UT-TIME']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hd['EXPTIME']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)',bcvel_baryc,'[km/s]') + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)',lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME',nombre) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',hd['RA']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',hd['DEC']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA-D',hd['RA-D']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC-D',hd['DEC-D']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',RA) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',DEC) + hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',hd['EQUINOX']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',hd['SITELAT']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',hd['SITELONG']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',hd['SITEALT']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',hd['AIRMASS']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON_VEL',refvel,'[km/s]') + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONST',moon_state) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LUNATION',lunation) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONSEP',moonsep2) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOONALT',float(mephem.alt)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SMOONALT',str(mephem.alt)) + + if (os.access(dirout+'proc/'+nf,os.F_OK)): + os.remove( dirout+'proc/'+nf ) + hdu.writeto( dirout+'proc/'+nf ) + ##################################################################################################################### ############################################### Spectral and RV analysis ############################################ ##################################################################################################################### @@ -1038,7 +1039,7 @@ spec_moon = np.array(spec_moon) use_moon = np.array(use_moon) -print 'Starting with the Post-processing of the spectra...' +print('Starting with the Post-processing of the spectra...') if not JustExtract: @@ -1047,7 +1048,7 @@ nombre = hd['OBJECT'] nama = dirout+'proc/' + nombre+'_'+hd['DATE-OBS']+'_'+hd['UT-TIME'][:2]+'-'+hd['UT-TIME'][3:5]+'-'+hd['UT-TIME'][6:] fit = nama+'_final.fits' - print '\n\t\tWorking on spectrum:', fit + print('\n\t\tWorking on spectrum:', fit) know_moon = False if fsim.split('/')[-1] in spec_moon: I = np.where(obj.split('/')[-1] == spec_moon)[0] @@ -1077,7 +1078,7 @@ SNR_5130 = np.median(spec[8,tuc][800:1200] ) if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') # spectral analysis # First, query SIMBAD with the object name query_success = False @@ -1085,16 +1086,16 @@ # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + nombre+'_'+hd['HIERARCH SHUTTER START DATE']+'_' \ - + hd['HIERARCH SHUTTER START UT'][:2]+'-'+hd['HIERARCH SHUTTER START UT'][3:5]+ \ - '-' + hd['HIERARCH SHUTTER START UT'][6:] + '_stellar_pars.txt' + + hd['HIERARCH SHUTTER START UT'][:2]+'-'+hd['HIERARCH SHUTTER START UT'][3:5]+ \ + '-' + hd['HIERARCH SHUTTER START UT'][6:] + '_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") spec2 = spec.copy() if resolution > 45000: Rx = np.around(1./np.sqrt(1./40000.**2 - 1./resolution**2)) @@ -1107,10 +1108,10 @@ f.write(line) f.close() else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -1121,17 +1122,17 @@ Z_epoch = Z vsini_epoch = vsini vel0_epoch = vel0 - + hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VEL0', vel0) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH TEFF', float(T_eff)) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH LOGG', float(logg)) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH Z', Z) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VSINI', vsini) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") first_o = 0 for i in range(spec.shape[1]): @@ -1146,7 +1147,7 @@ break spec1 = spec[:,first_o:last_o+1,:] - + # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) @@ -1172,16 +1173,16 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' + + print('\t\t\tComputing the CCF...') cond = True while (cond): #first rough correlation to find the minimum vels, xc_full, sn, nlines_ccf, W_ccf = GLOBALutils.XCor(spec1, ml_v, mh_v,\ - weight, 0, lbary_ltopo, vel_width=300, vel_step=3, start_order=0,\ - spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) + weight, 0, lbary_ltopo, vel_width=300, vel_step=3, start_order=0,\ + spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3, Simple=True, start_order=0, W=W_ccf) - #Normalize the continuum of the CCF robustly with R + #Normalize the continuum of the CCF robustly with R yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) @@ -1189,21 +1190,21 @@ xc_av /= pred pred_rough = pred.copy() - vel0_xc = vels[ np.argmin( xc_av ) ] + vel0_xc = vels[ np.argmin( xc_av ) ] rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ - xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec1, ml_v, mh_v, weight, vel0_xc, lbary_ltopo,\ - start_order=0, vel_width=vel_width, vel_step=0.1, spec_order=9, \ - iv_order=10, sn_order=8,max_vel_rough=300) - + GLOBALutils.XCor(spec1, ml_v, mh_v, weight, vel0_xc, lbary_ltopo,\ + start_order=0, vel_width=vel_width, vel_step=0.1, spec_order=9, \ + iv_order=10, sn_order=8,max_vel_rough=300) + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3,\ - Simple=True, W=W_ccf, start_order=0) + Simple=True, W=W_ccf, start_order=0) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred @@ -1216,8 +1217,8 @@ moon_sig = 4.5 p1,XCmodel,p1gau,XCmodelgau,Ls2 = \ - GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res=4,\ - horder=8, moonv = refvel, moons = moon_sig, moon = False) + GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res=4,\ + horder=8, moonv = refvel, moons = moon_sig, moon = False) moonmatters = False if (know_moon and here_moon): @@ -1237,23 +1238,23 @@ if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':TEXP} + 'lunation':lunation,'mephem':mephem,'texp':TEXP} pkl_xc = fit[:-4] + obname + '_XC_' + sp_type + '.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) @@ -1271,20 +1272,20 @@ else: D = 0.33491 C = 0.00113 - elif sp_type == 'K5': + elif sp_type == 'K5': D = 0.20695 C = 0.00321 - else: + else: D = 0.20695 C = 0.00321 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = 0.400 BSerr = np.around(D / float(np.round(SNR_5130)) + C,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1312,7 +1313,7 @@ line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f dupont ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, resolution, T_eff_epoch, logg_epoch,\ - Z_epoch, vsini_epoch, XC_min, disp_epoch, TEXP, SNR_5130_R, ccf_pdf) + Z_epoch, vsini_epoch, XC_min, disp_epoch, TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) hdu.close() -f_res.close() \ No newline at end of file +f_res.close() diff --git a/dupont/dupontutils.py b/dupont/dupontutils.py index 31f73ad..f588ed0 100644 --- a/dupont/dupontutils.py +++ b/dupont/dupontutils.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") base = '../' sys.path.append(base+"utils/GLOBALutils") @@ -22,371 +23,371 @@ def milk_comb(ImgList, darks, zero='Bias.fits'): - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - h = pyfits.open(ImgList[0])[0] - d = h.data - - head = pyfits.getheader(ImgList[0]) - expt = head['EXPTIME'] - - d = OverscanTrim(d,h.header['BIASSEC']) - - Master = pyfits.getdata(zero) - - if len(darks) > 0: - Dark = get_dark(darks,expt) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - - d -= Master - d -= Dark - - factor = 1.25 - if (n < 3): - factor = 1 - - ron1 = h.header['ENOISE'] - gain = h.header['EGAIN'] - - - ronoise = factor * h.header['ENOISE'] / np.sqrt(n) - - if (n == 1): - return d, ron1, gain - - else: - for i in range(n-1): - h = pyfits.open(ImgList[i+1])[0] - head = pyfits.getheader(ImgList[i+1]) - expt = head['EXPTIME'] - if len(darks) > 0: - Dark = get_dark(darks,expt) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - rd = OverscanTrim(h.data,h.header['BIASSEC']) - Master - Dark - d = np.dstack((d,rd/np.median(rd))) - out = np.median(d,axis=2) - return out, ronoise, gain - + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + h = pyfits.open(ImgList[0])[0] + d = h.data + + head = pyfits.getheader(ImgList[0]) + expt = head['EXPTIME'] + + d = OverscanTrim(d,h.header['BIASSEC']) + + Master = pyfits.getdata(zero) + + if len(darks) > 0: + Dark = get_dark(darks,expt) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + + d -= Master + d -= Dark + + factor = 1.25 + if (n < 3): + factor = 1 + + ron1 = h.header['ENOISE'] + gain = h.header['EGAIN'] + + + ronoise = factor * h.header['ENOISE'] / np.sqrt(n) + + if (n == 1): + return d, ron1, gain + + else: + for i in range(n-1): + h = pyfits.open(ImgList[i+1])[0] + head = pyfits.getheader(ImgList[i+1]) + expt = head['EXPTIME'] + if len(darks) > 0: + Dark = get_dark(darks,expt) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + rd = OverscanTrim(h.data,h.header['BIASSEC']) - Master - Dark + d = np.dstack((d,rd/np.median(rd))) + out = np.median(d,axis=2) + return out, ronoise, gain + def FileClassify(path,log): - biases = [] - milkflat = [] - objects = [] - darks = [] - thars = [] - skflat = [] - qflat = [] - f = open(log,'w') - archs = glob.glob(path+'ccd*.fits') - if os.access(path+'bad_files.txt',os.F_OK): - ff = open(path + 'bad_files.txt', 'r') - bfiles = ff.readlines() - else: - bfiles = [] - for arch in archs: - use = True - for bf in bfiles: - if arch == path + bf[:-1]: - print 'Dumped file', arch - use = False - break - if use: - h = pyfits.open(arch) - header = pyfits.getheader(arch) - name = header['OBJECT'] - - if True: - if header['EXPTYPE'] == 'Object': - - if name.count('sky')>0: - skflat.append(arch) - elif (name.lower()).count('milky')>0: - milkflat.append(arch) - elif name.count('qFlat')>0: - qflat.append(arch) - else: - expt = header['EXPTIME'] - ra = header['RA'] - dec = header['DEC'] - ams = header['AIRMASS'] - date = header['DATE-OBS'] - UT = header['UT-TIME'] - line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (name, ra, dec, expt, ams, date, UT, arch) - f.write(line) - cos = name.split('ThAr') - if 'thar' in name.lower() or len(cos)> 1 or 'comp' in name.lower(): - thars.append(arch) - elif name.count('milky')>0 or name.count('Milky')>0 or name.count('flat')>0: - milkflat.append(arch) - else: - objects.append(arch) - elif header['EXPTYPE'] == 'Bias': - biases.append(arch) - elif header['EXPTYPE'] == 'Flat': - nam = header['OBJECT'] - if nam.count('sky')>0 or nam.count('Sky')>0 : - skflat.append(arch) - elif nam.count('milky')>0 or nam.count('Milky')>0 or nam.count('flat')>0: - milkflat.append(arch) - elif header['EXPTYPE'] == 'Dark': - darks.append(arch) - - h.close() - f.close() - return biases, milkflat, skflat, objects, thars, darks + biases = [] + milkflat = [] + objects = [] + darks = [] + thars = [] + skflat = [] + qflat = [] + f = open(log,'w') + archs = glob.glob(path+'ccd*.fits') + if os.access(path+'bad_files.txt',os.F_OK): + ff = open(path + 'bad_files.txt', 'r') + bfiles = ff.readlines() + else: + bfiles = [] + for arch in archs: + use = True + for bf in bfiles: + if arch == path + bf[:-1]: + print('Dumped file', arch) + use = False + break + if use: + h = pyfits.open(arch) + header = pyfits.getheader(arch) + name = header['OBJECT'] + + if True: + if header['EXPTYPE'] == 'Object': + + if name.count('sky')>0: + skflat.append(arch) + elif (name.lower()).count('milky')>0: + milkflat.append(arch) + elif name.count('qFlat')>0: + qflat.append(arch) + else: + expt = header['EXPTIME'] + ra = header['RA'] + dec = header['DEC'] + ams = header['AIRMASS'] + date = header['DATE-OBS'] + UT = header['UT-TIME'] + line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (name, ra, dec, expt, ams, date, UT, arch) + f.write(line) + cos = name.split('ThAr') + if 'thar' in name.lower() or len(cos)> 1 or 'comp' in name.lower(): + thars.append(arch) + elif name.count('milky')>0 or name.count('Milky')>0 or name.count('flat')>0: + milkflat.append(arch) + else: + objects.append(arch) + elif header['EXPTYPE'] == 'Bias': + biases.append(arch) + elif header['EXPTYPE'] == 'Flat': + nam = header['OBJECT'] + if nam.count('sky')>0 or nam.count('Sky')>0 : + skflat.append(arch) + elif nam.count('milky')>0 or nam.count('Milky')>0 or nam.count('flat')>0: + milkflat.append(arch) + elif header['EXPTYPE'] == 'Dark': + darks.append(arch) + + h.close() + f.close() + return biases, milkflat, skflat, objects, thars, darks def MedianCombine(ImgList, zero_bo=False, zero='Bias.fits', dark_bo=False, darks=[], flat_bo=False, flat='Flat.fits'): - """ - Median combine a list of images - """ - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - - h = pyfits.open(ImgList[0])[0] - d = h.data - d = OverscanTrim(d,h.header['BIASSEC']) - - if zero_bo: - Master = pyfits.getdata(zero) - else: - Master = np.zeros((d.shape[0],d.shape[1]),float) - - if dark_bo and len(darks)!=0: - hd = pyfits.getheader(ImgList[0]) - time = hd['EXPTIME'] - Dark = get_dark(darks, time) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - - if flat_bo: - Flat = pyfits.getdata(flat) - else: - Flat = np.zeros((d.shape[0],d.shape[1]),float) + 1.0 - - if flat_bo: - d = (d - Master - Dark)/Flat - else: - d = (d - Master - Dark) - - factor = 1.25 - - if (n < 3): - factor = 1 - - ronoise = factor * h.header['ENOISE'] / np.sqrt(n) - gain = h.header['EGAIN'] - - if (n == 1): - return d, ronoise, gain - - else: - for i in range(n-1): - h = pyfits.open(ImgList[i+1])[0] - if flat_bo: - d = np.dstack((d,(OverscanTrim(h.data,h.header['BIASSEC'])-Master-Dark)/Flat)) - else: - - d = np.dstack((d,OverscanTrim(h.data,h.header['BIASSEC'])-Master-Dark)) - - return np.median(d,axis=2), ronoise, gain + """ + Median combine a list of images + """ + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + + h = pyfits.open(ImgList[0])[0] + d = h.data + d = OverscanTrim(d,h.header['BIASSEC']) + + if zero_bo: + Master = pyfits.getdata(zero) + else: + Master = np.zeros((d.shape[0],d.shape[1]),float) + + if dark_bo and len(darks)!=0: + hd = pyfits.getheader(ImgList[0]) + time = hd['EXPTIME'] + Dark = get_dark(darks, time) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + + if flat_bo: + Flat = pyfits.getdata(flat) + else: + Flat = np.zeros((d.shape[0],d.shape[1]),float) + 1.0 + + if flat_bo: + d = (d - Master - Dark)/Flat + else: + d = (d - Master - Dark) + + factor = 1.25 + + if (n < 3): + factor = 1 + + ronoise = factor * h.header['ENOISE'] / np.sqrt(n) + gain = h.header['EGAIN'] + + if (n == 1): + return d, ronoise, gain + + else: + for i in range(n-1): + h = pyfits.open(ImgList[i+1])[0] + if flat_bo: + d = np.dstack((d,(OverscanTrim(h.data,h.header['BIASSEC'])-Master-Dark)/Flat)) + else: + + d = np.dstack((d,OverscanTrim(h.data,h.header['BIASSEC'])-Master-Dark)) + + return np.median(d,axis=2), ronoise, gain def OverscanTrim(d,bsec): - """ - Overscan correct and Trim a refurbished DuPont image - """ - bsec = bsec[1:-1] - bsec1 = bsec.split(',')[0] - bsec2 = bsec.split(',')[1] - b11 = int(bsec1.split(':')[0]) - b12 = int(bsec1.split(':')[1]) - b21 = int(bsec2.split(':')[0]) - b22 = int(bsec2.split(':')[1]) - - t1 = d[:1500,:b11-1] - t2 = d[b21-1:,:b11-1] - - nd = np.zeros((t1.shape[0],t1.shape[1]),float) - nd[:1500,:] = t1 - - overscan1 = np.median(t2,axis=0) - newdata = nd - overscan1 - - return newdata + """ + Overscan correct and Trim a refurbished DuPont image + """ + bsec = bsec[1:-1] + bsec1 = bsec.split(',')[0] + bsec2 = bsec.split(',')[1] + b11 = int(bsec1.split(':')[0]) + b12 = int(bsec1.split(':')[1]) + b21 = int(bsec2.split(':')[0]) + b22 = int(bsec2.split(':')[1]) + + t1 = d[:1500,:b11-1] + t2 = d[b21-1:,:b11-1] + + nd = np.zeros((t1.shape[0],t1.shape[1]),float) + nd[:1500,:] = t1 + + overscan1 = np.median(t2,axis=0) + newdata = nd - overscan1 + + return newdata def get_dark(darks,t): - exact = 0 - dts = [] - for dark in darks: - hd = pyfits.getheader(dark) - dt = hd['EXPTIME'] - dts.append(dt) - if dt == t: - DARK = pyfits.getdata(dark) - exact = 1 - - dts = np.array(dts) - if exact == 0: - if t < dts.min(): - I = np.where( dts == dts.min() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - elif t > dts.max(): - I = np.where( dts == dts.max() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - else: - tmin = dts.min() - tmax = dts.max() - I = np.where( dts == dts.min() )[0] - Dmin = pyfits.getdata(darks[I[0]]) - Dminname=darks[I[0]] - I = np.where( dts == dts.max() )[0] - Dmax = pyfits.getdata(darks[I[0]]) - Dmaxname = darks[I[0]] - - i = 0 - while i < len(dts): - if dts[i] < t and dts[i] > tmin: - tmin = dts[i] - Dminname = darks[i] - Dmin = pyfits.getdata(darks[i]) - elif dts[i] > t and dts[i] < tmax: - tmax = dts[i] - Dmaxname = darks[i] - Dmax = pyfits.getdata(darks[i]) - i+=1 - - num = Dmax - Dmin - den = tmax-tmin - m = num/den - n = Dmax - m*tmax - DARK = m*t+n - - return DARK + exact = 0 + dts = [] + for dark in darks: + hd = pyfits.getheader(dark) + dt = hd['EXPTIME'] + dts.append(dt) + if dt == t: + DARK = pyfits.getdata(dark) + exact = 1 + + dts = np.array(dts) + if exact == 0: + if t < dts.min(): + I = np.where( dts == dts.min() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + elif t > dts.max(): + I = np.where( dts == dts.max() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + else: + tmin = dts.min() + tmax = dts.max() + I = np.where( dts == dts.min() )[0] + Dmin = pyfits.getdata(darks[I[0]]) + Dminname=darks[I[0]] + I = np.where( dts == dts.max() )[0] + Dmax = pyfits.getdata(darks[I[0]]) + Dmaxname = darks[I[0]] + + i = 0 + while i < len(dts): + if dts[i] < t and dts[i] > tmin: + tmin = dts[i] + Dminname = darks[i] + Dmin = pyfits.getdata(darks[i]) + elif dts[i] > t and dts[i] < tmax: + tmax = dts[i] + Dmaxname = darks[i] + Dmax = pyfits.getdata(darks[i]) + i+=1 + + num = Dmax - Dmin + den = tmax-tmin + m = num/den + n = Dmax - m*tmax + DARK = m*t+n + + return DARK def get_blaze(LL,FF, low=1.0, hi=3.0, n = 6): - NF = FF.copy() - for j in range(LL.shape[0]): - L = LL[j] - F = FF[j] - ejex = np.arange(len(F)) - F[:150] = 0.0 - F[-150:] = 0.0 - Z = np.where(F!=0)[0] - F = scipy.signal.medfilt(F[Z],31) - ejexx = ejex.copy() - ejex = ejex[Z] - L = L[Z] - I = np.where((L>5870) & (L<5890))[0] - if len(I)>0: - W = np.where(L<5870)[0] - R = np.where(L>5890)[0] - ejetemp = np.hstack((ejex[W],ejex[R])) - Ftemp = np.hstack((F[W],F[R])) - coefs = np.polyfit(ejetemp,Ftemp,n) - fit = np.polyval(coefs,ejetemp) - - else: - ejetemp=ejex - Ftemp=F - coefs = np.polyfit(ejex,F,n) - fit = np.polyval(coefs,ejex) - i = 0 - while i < 30: - res = Ftemp - fit - IP = np.where((res>=0) & (Ftemp!=0.0))[0] - IN = np.where((res<0) & (Ftemp!=0.0))[0] - devp = np.mean(res[IP]) - devn = np.mean(res[IN]) - I = np.where((res > -low*abs(devn)) & (res < hi*abs(devp)) & (Ftemp!=0))[0] - coefs = np.polyfit(ejetemp[I],Ftemp[I],n) - fit = np.polyval(coefs,ejetemp) - i+=1 - fit = np.polyval(coefs,ejexx) - - NF[j]=fit - NNF = NF.copy() - for j in range(LL.shape[0]): - - L = LL[j] - I = np.where((L>6520) & (L<6600))[0] - if len(I)>0: - - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j+1 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) - - - I = np.where((L>4870) & (L<4880))[0] - if len(I)>0: - - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j+1 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - else: - for i in range(len(L)): - vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) - - I = np.where((L>4320) & (L<4325))[0] - if len(I)>0: - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - return NNF + NF = FF.copy() + for j in range(LL.shape[0]): + L = LL[j] + F = FF[j] + ejex = np.arange(len(F)) + F[:150] = 0.0 + F[-150:] = 0.0 + Z = np.where(F!=0)[0] + F = scipy.signal.medfilt(F[Z],31) + ejexx = ejex.copy() + ejex = ejex[Z] + L = L[Z] + I = np.where((L>5870) & (L<5890))[0] + if len(I)>0: + W = np.where(L<5870)[0] + R = np.where(L>5890)[0] + ejetemp = np.hstack((ejex[W],ejex[R])) + Ftemp = np.hstack((F[W],F[R])) + coefs = np.polyfit(ejetemp,Ftemp,n) + fit = np.polyval(coefs,ejetemp) + + else: + ejetemp=ejex + Ftemp=F + coefs = np.polyfit(ejex,F,n) + fit = np.polyval(coefs,ejex) + i = 0 + while i < 30: + res = Ftemp - fit + IP = np.where((res>=0) & (Ftemp!=0.0))[0] + IN = np.where((res<0) & (Ftemp!=0.0))[0] + devp = np.mean(res[IP]) + devn = np.mean(res[IN]) + I = np.where((res > -low*abs(devn)) & (res < hi*abs(devp)) & (Ftemp!=0))[0] + coefs = np.polyfit(ejetemp[I],Ftemp[I],n) + fit = np.polyval(coefs,ejetemp) + i+=1 + fit = np.polyval(coefs,ejexx) + + NF[j]=fit + NNF = NF.copy() + for j in range(LL.shape[0]): + + L = LL[j] + I = np.where((L>6520) & (L<6600))[0] + if len(I)>0: + + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j+1 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) + + + I = np.where((L>4870) & (L<4880))[0] + if len(I)>0: + + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j+1 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + else: + for i in range(len(L)): + vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) + + I = np.where((L>4320) & (L<4325))[0] + if len(I)>0: + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + return NNF def get_close(tht,rat,dect,fits): - t0 = 1000000. - close = fits[0] - for fit in fits: - #print close - hd = pyfits.getheader(fit) - sct,mjd0 = mjd_fromheader(hd) - expt = hd['EXPTIME']/(3600.*24.) - dec = hd['DEC-D'] - ra = hd['RA-D'] - if abs(dec - dect)<0.05 and abs(ra - rat)<0.05: - #print sct+expt,tht - if abs(sct+expt-tht) < t0: - t0 = abs(sct+expt-tht) - close = fit - return close - - + t0 = 1000000. + close = fits[0] + for fit in fits: + #print close + hd = pyfits.getheader(fit) + sct,mjd0 = mjd_fromheader(hd) + expt = hd['EXPTIME']/(3600.*24.) + dec = hd['DEC-D'] + ra = hd['RA-D'] + if abs(dec - dect)<0.05 and abs(ra - rat)<0.05: + #print sct+expt,tht + if abs(sct+expt-tht) < t0: + t0 = abs(sct+expt-tht) + close = fit + return close + + def b_col(d): - d[:,746] = 0.5*(d[:,745]+d[:,748]) - d[:,747] = 0.5*(d[:,745]+d[:,748]) - return d + d[:,746] = 0.5*(d[:,745]+d[:,748]) + d[:,747] = 0.5*(d[:,745]+d[:,748]) + return d def mjd_fromheader(h): """ return modified Julian date from header - """ + """ datetu = h['UT-DATE'] timetu = h['UT-TIME'] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:])) @@ -402,4 +403,4 @@ def mjd_fromheader(h): mjd = mjd_start + (fraction * texp) / secinday - return mjd, mjd0 \ No newline at end of file + return mjd, mjd0 diff --git a/espadons/espadonspipe.py b/espadons/espadonspipe.py index 0ab47d1..43a3ec8 100644 --- a/espadons/espadonspipe.py +++ b/espadons/espadonspipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * @@ -13,7 +14,7 @@ ephemeris='DEc403' import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages @@ -84,7 +85,7 @@ ####### GLOBAL VARIABLES ##### ## perhaps put into options ## force_pre_process = False -force_flat_extract = False +force_flat_extract = False force_thar_extract = False force_thar_wavcal = False force_tharxc = False @@ -106,7 +107,7 @@ NSigma_Marsh = 10 NCosmic_Marsh = 10 S_Marsh = 0.4 -N_Marsh = 4 # grado polinomio +N_Marsh = 4 # grado polinomio min_extract_col = 50 max_extract_col = 4600 @@ -119,7 +120,7 @@ models_path = base+"data/COELHO_MODELS/R_40000b/" order_dir = base+"espadons/wavcals/" - + OO0 = 22 RES = 80000. @@ -130,10 +131,10 @@ # file containing the log log = dirout+'night.log' -print "\n\n\tESPaDOnS CFHT3.6m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tESPaDOnS CFHT3.6m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') biases, flats, ThAr_ref, sim_sci, ThAr_ref_dates = espadonsutils.FileClassify(dirin,log,mode=mode,amps=amps) ThAr_ref = ThAr_ref[:2] @@ -142,29 +143,29 @@ (os.access(dirout+'trace_'+mode+'.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias_'+mode+'.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): - print "\t\tGenerating Master calibration frames..." + print("\t\tGenerating Master calibration frames...") # median combine Biases MasterBias, RO_bias, GA_bias = espadonsutils.MedianCombine(biases,zero_bo=False) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias_'+mode+'.fits',os.F_OK)): os.remove(dirout+'MasterBias_'+mode+'.fits') hdu.writeto(dirout+'MasterBias_'+mode+'.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") - # median combine list of flats + # median combine list of flats Flat, RO_flat, GA_flat = espadonsutils.MedianCombine(flats, zero_bo=True, zero=dirout+'MasterBias_'+mode+'.fits') hdu = pyfits.PrimaryHDU( Flat ) if (os.access(dirout+'Flat_'+mode+'.fits',os.F_OK)): os.remove(dirout+'Flat_'+mode+'.fits') hdu.writeto(dirout+'Flat_'+mode+'.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") GFlat = Flat.copy() if mode == 'so': d1 = np.loadtxt('mid_co_so.txt') @@ -182,11 +183,11 @@ GFlat[i] = scipy.ndimage.filters.gaussian_filter(Flat[i],2) GFlat = GFlat.T - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") if mode == 'so': c_all = espadonsutils.get_them(GFlat, 14, trace_degree,mode=0, shift=traces_shift) nord_all = len(c_all) - print "\t\t"+str(nord_all)+" orders traced..." + print("\t\t"+str(nord_all)+" orders traced...") trace_dict = {'c_all':c_all, \ 'nord_all':nord_all,\ 'GA_flat':GA_flat,'RO_flat':RO_flat} @@ -196,8 +197,8 @@ nord_ob, nord_co = len(c_ob), len(c_co) c_all = GLOBALutils.Mesh(c_ob,c_co) nord_all = len(c_all) - print "\t\t"+str(nord_ob)+" object orders traced..." - print "\t\t"+str(nord_co)+" comparison orders traced..." + print("\t\t"+str(nord_ob)+" object orders traced...") + print("\t\t"+str(nord_co)+" comparison orders traced...") trace_dict = {'c_all':c_all, 'c_ob':c_ob, 'c_co':c_co, \ 'nord_all':nord_all, 'nord_ob':nord_ob, 'nord_co':nord_co, \ 'GA_flat':GA_flat,'RO_flat':RO_flat} @@ -237,7 +238,7 @@ show() """ -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') Flat = Flat.T P_fits = dirout + 'P.fits' P_ob_fits = dirout + 'P_ob.fits' @@ -294,14 +295,14 @@ os.remove(P_co_fits) hdu = pyfits.PrimaryHDU( P_co ) hdu.writeto( P_co_fits ) - - print "\t\t\tNo extracted flat spectra found or extraction forced, extracting and saving..." + + print("\t\t\tNo extracted flat spectra found or extraction forced, extracting and saving...") if mode == 'so': S_flat = GLOBALutils.optimal_extraction(Flat.T,P,c_all,ext_aperture,\ RO_flat,GA_flat,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) if (os.access(S_flat_fits,os.F_OK)): - os.remove( S_flat_fits ) + os.remove( S_flat_fits ) hdu = pyfits.PrimaryHDU( S_flat ) hdu.writeto( S_flat_fits ) else: @@ -313,24 +314,24 @@ min_extract_col,max_extract_col,npools) # write P_on and S_flat_ob as fits files if (os.access(S_flat_ob_fits,os.F_OK)): - os.remove( S_flat_ob_fits ) + os.remove( S_flat_ob_fits ) hdu = pyfits.PrimaryHDU( S_flat_ob ) hdu.writeto( S_flat_ob_fits ) if (os.access(S_flat_co_fits,os.F_OK)): - os.remove( S_flat_co_fits ) + os.remove( S_flat_co_fits ) hdu = pyfits.PrimaryHDU( S_flat_co ) hdu.writeto( S_flat_co_fits ) - + else: - print "\t\tExtracted flat spectra found, loading..." + print("\t\tExtracted flat spectra found, loading...") if mode == 'so': P = pyfits.getdata( P_fits ) S_flat = pyfits.getdata( S_flat_fits ) else: P_ob = pyfits.getdata( P_ob_fits ) - S_flat_ob = pyfits.getdata( S_flat_ob_fits ) + S_flat_ob = pyfits.getdata( S_flat_ob_fits ) P_co = pyfits.getdata( P_co_fits ) - S_flat_co = pyfits.getdata( S_flat_co_fits ) + S_flat_co = pyfits.getdata( S_flat_co_fits ) if mode == 'so': S_flat_n, Snorms = GLOBALutils.FlatNormalize_single( S_flat, mid=int(.5*S_flat.shape[2])) @@ -338,10 +339,10 @@ S_flat_ob_n, Snorms_ob = GLOBALutils.FlatNormalize_single( S_flat_ob, mid=int(.5*S_flat_ob.shape[2])) S_flat_co_n, Snorms_co = GLOBALutils.FlatNormalize_single( S_flat_co, mid=int(.5*S_flat_co.shape[2])) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr+Ne files for fsim in ThAr_ref: - print "\t\tWorking on ThAr+Ne file ", fsim, "..." + print("\t\tWorking on ThAr+Ne file ", fsim, "...") hthar = pyfits.open( fsim ) dthar = espadonsutils.OverscanTrim(pyfits.getdata(fsim)) @@ -354,7 +355,7 @@ thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' thar_co_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' do_thar = False - if mode == 'so' and (os.access(thar_fits,os.F_OK) == False or os.access(thar_fits_simple,os.F_OK) == False): + if mode == 'so' and (os.access(thar_fits,os.F_OK) == False or os.access(thar_fits_simple,os.F_OK) == False): do_thar = True if mode != 'so' and (os.access(thar_ob_fits,os.F_OK) == False or os.access(thar_ob_fits_simple,os.F_OK) == False or\ os.access(thar_co_fits,os.F_OK) == False or os.access(thar_co_fits_simple,os.F_OK) == False ): @@ -375,8 +376,8 @@ dthar = dthar.T RO_thar, GA_thar = hthar[0].header['RDNOISEA'],hthar[0].header['GAINA'] - - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") if mode == 'so': thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) @@ -385,7 +386,7 @@ os.remove( thar_fits ) if (os.access(thar_fits_simple,os.F_OK)): os.remove( thar_fits_simple ) - + hdu = pyfits.PrimaryHDU( thar_S ) hdu.writeto( thar_fits ) hdu = pyfits.PrimaryHDU( thar_Ss ) @@ -413,19 +414,19 @@ hdu = pyfits.PrimaryHDU( thar_Ss_co ) hdu.writeto( thar_co_fits_simple ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") sorted_ThAr_dates = np.argsort( ThAr_ref_dates ) -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") if mode == 'so': for i in range(len(sorted_ThAr_dates)): - index = sorted_ThAr_dates[i] + index = sorted_ThAr_dates[i] hd = pyfits.getheader(ThAr_ref[index]) wavsol_pkl = dirout + ThAr_ref[index].split('/')[-1][:-4]+'wavsolpars.pkl' - + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tComputing wavelength solution of ThAr file", ThAr_ref[index] + print("\t\tComputing wavelength solution of ThAr file", ThAr_ref[index]) hthar = pyfits.open( ThAr_ref[index] ) mjd, mjd0 = espadonsutils.mjd_fromheader( hthar ) @@ -435,7 +436,7 @@ lines_thar = thar_S[:,1,:] iv_thar = thar_S[:,2,:] - + All_Pixel_Centers = np.array([]) All_Wavelengths = np.array([]) All_Orders = np.array([]) @@ -449,7 +450,7 @@ order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar[order,:] IV = iv_thar[order,:] wei = np.sqrt( IV ) @@ -465,7 +466,7 @@ #print order #plot(pixel_centers, wavelengths,'ro') #show() - if (order == 20): + if (order == 20): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(.5*len(thar_order)), len(thar_order) ) else: @@ -497,16 +498,16 @@ pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) else: for i in range(len(sorted_ThAr_dates)): - index = sorted_ThAr_dates[i] + index = sorted_ThAr_dates[i] hd = pyfits.getheader(ThAr_ref[index]) wavsol_pkl = dirout + ThAr_ref[index].split('/')[-1][:-4]+'wavsolpars.pkl' - + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tComputing wavelength solution of ThAr file", ThAr_ref[index] + print("\t\tComputing wavelength solution of ThAr file", ThAr_ref[index]) hthar = pyfits.open( ThAr_ref[index] ) mjd, mjd0 = espadonsutils.mjd_fromheader( hthar ) @@ -521,7 +522,7 @@ iv_thar_ob = thar_S_ob[:,2,:] lines_thar_co = thar_S_ob[:,1,:] iv_thar_co = thar_S_ob[:,2,:] - + All_Pixel_Centers = np.array([]) All_Wavelengths = np.array([]) All_Orders = np.array([]) @@ -543,7 +544,7 @@ order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar_ob[order,:] IV = iv_thar_ob[order,:] wei = np.sqrt( IV ) @@ -570,7 +571,7 @@ #print order #plot(pixel_centers, wavelengths,'ro') #show() - if (order == 20): + if (order == 20): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(.5*len(thar_order)), len(thar_order) ) else: @@ -620,7 +621,7 @@ pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) wavsol_pkl = dirout + ThAr_ref[0].split('/')[-1][:-4]+'wavsolpars.pkl' dct = pickle.load(open(wavsol_pkl,'r')) @@ -691,15 +692,15 @@ know_moon = True here_moon = use_moon[I] - print '\n' - print "\t--> Working on image: ", fsim + print('\n') + print("\t--> Working on image: ", fsim) h = pyfits.open(fsim) mjd,mjd0 = espadonsutils.mjd_fromheader(h) ronoise, gain = float(h[0].header['RDNOISEA']),float(h[0].header['GAINA']) # Object name obname = h[0].header['OBJNAME'] - print "\t\tObject name:",obname + print("\t\tObject name:",obname) data = espadonsutils.OverscanTrim( h[0].data ) - MasterBias data = data.T @@ -737,7 +738,7 @@ ra = ra2 dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -747,13 +748,13 @@ res = jplephem.doppler_fraction(float(ra/15.0), float(dec), long(mjd), float(mjd%1), 1, 0.0) lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) - gobs = ephem.Observer() + gobs = ephem.Observer() gobs.name = h[0].header['TELESCOP'] - gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.lat = rad(latitude) # lat/long in decimal degrees gobs.long = rad(longitude) timeT = h[0].header['UTC-OBS'].split(':') if len(timeT[0]) == 1: @@ -769,11 +770,11 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) #moon_alts.update({fsim:mephem.alt}) #moon_ills.update({fsim:lunation}) - print '\t\tExtraction:' + print('\t\tExtraction:') if mode == 'so': sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' @@ -787,7 +788,7 @@ sci_S = GLOBALutils.optimal_extraction(data,P,c_all,ext_aperture,\ ronoise,gain,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - + if (os.access(sci_fits,os.F_OK)): os.remove( sci_fits ) if (os.access(sci_fits_simple,os.F_OK)): @@ -797,10 +798,10 @@ hdu.writeto( sci_fits ) hdu = pyfits.PrimaryHDU( sci_Ss ) hdu.writeto( sci_fits_simple ) - + else: - print '\t\t\t '+fsim+" has already been extracted, reading in product fits files..." + print('\t\t\t '+fsim+" has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) else: @@ -824,7 +825,7 @@ sci_co_S = GLOBALutils.optimal_extraction(data,P_co,c_co,ext_aperture,\ ronoise,gain,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - + if (os.access(sci_ob_fits,os.F_OK)): os.remove( sci_ob_fits ) if (os.access(sci_ob_fits_simple,os.F_OK)): @@ -842,10 +843,10 @@ hdu.writeto( sci_co_fits ) hdu = pyfits.PrimaryHDU( sci_co_Ss ) hdu.writeto( sci_co_fits_simple ) - + else: - print '\t\t\t '+fsim+" has already been extracted, reading in product fits files..." + print('\t\t\t '+fsim+" has already been extracted, reading in product fits files...") if mode == 'so': sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) @@ -871,7 +872,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['UTC-OBS']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -893,7 +894,7 @@ hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH SHUTTER START UT', h[0].header['UTC-OBS']) hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH TARGET NAME', obname) hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH RA',h[0].header['RA']) hdu_co = GLOBALutils.update_header(hdu_co,'HIERARCH DEC',h[0].header['DEC']) @@ -916,7 +917,7 @@ Snorms = Snorms_ob.copy() S_flat = S_flat / Snorms.max() Snorms = Snorms / Snorms.max() - print '\t\tWavelength calibration:' + print('\t\tWavelength calibration:') #print "\t\t\tInstrumental drift:",(1e-6*p_shift)*299792458.0 # Apply new wavelength solution including barycentric correction equis = np.arange( data.shape[1] ) @@ -942,7 +943,7 @@ ccoef = GLOBALutils.get_cont_single(spec[0,ind],spec[3,ind],spec[4,ind],ll=1.5,lu=5,nc=nconts[ind]) L = np.where( spec[1,ind] != 0 ) - spec[5,ind,:][L] = spec[3,ind][L] / np.polyval(ccoef,spec[0,ind][L]) + spec[5,ind,:][L] = spec[3,ind][L] / np.polyval(ccoef,spec[0,ind][L]) ratio = np.polyval(ccoef,spec[0,ind][L]) * Snorms[ind] spec[6,ind,:][L] = spec[4,ind][L] * (ratio ** 2 ) spec[7,ind,:][L] = ratio @@ -954,7 +955,7 @@ LL = np.where(spec[5,ind] > 1 + 10. / scipy.signal.medfilt(spec[8,ind],21))[0] spec[5,ind,LL] = 1. - spec[9,ind][L] = spec[5,ind][L] * (dlambda_dx[L] ** 1) + spec[9,ind][L] = spec[5,ind][L] * (dlambda_dx[L] ** 1) spec[10,ind][L] = spec[6,ind][L] / (dlambda_dx[L] ** 2) ind +=1 @@ -991,7 +992,7 @@ ccoef = GLOBALutils.get_cont_single(spec[0,ind],spec[3,ind],spec[4,ind],ll=1.5,lu=5,nc=nconts[ind]) L = np.where( spec_co[1,ind] != 0 ) - spec_co[5,ind,:][L] = spec_co[3,ind][L] / np.polyval(ccoef,spec_co[0,ind][L]) + spec_co[5,ind,:][L] = spec_co[3,ind][L] / np.polyval(ccoef,spec_co[0,ind][L]) ratio = np.polyval(ccoef,spec_co[0,ind][L]) * Snorms[ind] spec_co[6,ind,:][L] = spec_co[4,ind][L] * (ratio ** 2 ) spec_co[7,ind,:][L] = ratio @@ -1003,7 +1004,7 @@ LL = np.where(spec_co[5,ind] > 1 + 10. / scipy.signal.medfilt(spec_co[8,ind],21))[0] spec_co[5,ind,LL] = 1. - spec_co[9,ind][L] = spec_co[5,ind][L] * (dlambda_dx[L] ** 1) + spec_co[9,ind][L] = spec_co[5,ind][L] * (dlambda_dx[L] ** 1) spec_co[10,ind][L] = spec_co[6,ind][L] / (dlambda_dx[L] ** 2) ind +=1 @@ -1014,7 +1015,7 @@ if (not JustExtract): if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') # spectral analysis # First, query SIMBAD with the object name query_success = False @@ -1022,14 +1023,14 @@ # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + fsim.split('/')[-1][:-4]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./float(RES)**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -1040,12 +1041,12 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -1061,16 +1062,16 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= (av_m - ml_v) @@ -1091,9 +1092,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): @@ -1103,7 +1104,7 @@ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - # Normalize the continuum of the CCF robustly with R + # Normalize the continuum of the CCF robustly with R yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) @@ -1126,7 +1127,7 @@ xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred - + if sp_type == 'M5': moon_sig = 2.5 elif sp_type == 'K5': @@ -1156,11 +1157,11 @@ if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -1200,12 +1201,12 @@ C = 0.00113 elif sp_type == 'K5': A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 else: A = 0.05348 - B = 0.00147 + B = 0.00147 D = 0.20695 C = 0.00321 @@ -1226,13 +1227,13 @@ BSerr = D / float(np.round(SNR_5130)) + C - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr2,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1263,12 +1264,12 @@ (obname, bjd_out, RV, RVerr2, BS, BSerr, RES, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) - + if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) else: - print "Reading spectral file from", fout + print("Reading spectral file from", fout) spec = pyfits.getdata( fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/espadons/espadonsutils.py b/espadons/espadonsutils.py index 9bbeee0..efd9245 100644 --- a/espadons/espadonsutils.py +++ b/espadons/espadonsutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from astropy.io import fits as pyfits import numpy as np from numpy import median,sqrt,array,exp @@ -18,99 +19,99 @@ def FileClassify(diri, log,mode='so',amps='a'): - """ - Classifies all files in a directory and writes a night log of science images - """ - if mode == 'so': - md = 'star only' - elif mode == 'ss': - md = 'star+sky' - else: - md = 'Polarimetry' - - if amps == 'a': - samps = 'a' - elif amps == 'ab': - samps = 'a,b' - - # define output lists - simThAr_sci = [] - simSky_sci = [] - biases = [] - bias_dates = [] - flat_dates = [] - flats = [] - ThAr_ref = [] - ThAr_ref_dates = [] - mode = [] - - f = open(log,'w') - bad_files = [] - if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - - all_files = glob.glob(diri+"*fits") - - jj = 0 - - for archivo in all_files: - jj+=1 - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - - if not dump: - h = pyfits.open(archivo) - #print archivo, h[0].header['EXPTYPE'], h[0].header['INSTMODE'] - if md in h[0].header['INSTMODE'] and samps == h[0].header['AMPLIST']: - print archivo, h[0].header['EXPTYPE'], h[0].header['INSTMODE'], h[0].data.shape, h[0].header['AMPLIST'] - if h[0].header['EXPTYPE'] == 'OBJECT': - simThAr_sci.append(archivo) - obname = h[0].header['OBJNAME'] - ra = h[0].header['OBJRA'] - delta = h[0].header['OBJDEC'] - airmass= h[0].header['AIRMASS'] - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'] + h[0].header['UTC-OBS'] - line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['EXPTYPE'], date, archivo) - f.write(line) - - elif h[0].header['EXPTYPE'] == 'BIAS': - biases.append(archivo) - mjd,mjd0 = mjd_fromheader(h) - bias_dates.append(mjd) - - elif h[0].header['EXPTYPE'] == 'FLAT': - flats.append(archivo) - mjd,mjd0 = mjd_fromheader(h) - flat_dates.append(mjd) - - elif h[0].header['EXPTYPE'] == 'COMPARISON': - mjd, mjd0 = mjd_fromheader(h) - ThAr_ref.append(archivo) - ThAr_ref_dates.append( mjd ) - - f.close() - biases, bias_dates = np.array(biases), np.array(bias_dates) - flats, flat_dates = np.array(flats), np.array(flat_dates) - IS = np.argsort(bias_dates) - biases, bias_dates = biases[IS], bias_dates[IS] - IS = np.argsort(flat_dates) - flats, flat_dates = flats[IS], flat_dates[IS] - - return biases, flats, ThAr_ref, simThAr_sci, ThAr_ref_dates + """ + Classifies all files in a directory and writes a night log of science images + """ + if mode == 'so': + md = 'star only' + elif mode == 'ss': + md = 'star+sky' + else: + md = 'Polarimetry' + + if amps == 'a': + samps = 'a' + elif amps == 'ab': + samps = 'a,b' + + # define output lists + simThAr_sci = [] + simSky_sci = [] + biases = [] + bias_dates = [] + flat_dates = [] + flats = [] + ThAr_ref = [] + ThAr_ref_dates = [] + mode = [] + + f = open(log,'w') + bad_files = [] + if os.access(diri+'bad_files.txt',os.F_OK): + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + + all_files = glob.glob(diri+"*fits") + + jj = 0 + + for archivo in all_files: + jj+=1 + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + + if not dump: + h = pyfits.open(archivo) + #print archivo, h[0].header['EXPTYPE'], h[0].header['INSTMODE'] + if md in h[0].header['INSTMODE'] and samps == h[0].header['AMPLIST']: + print(archivo, h[0].header['EXPTYPE'], h[0].header['INSTMODE'], h[0].data.shape, h[0].header['AMPLIST']) + if h[0].header['EXPTYPE'] == 'OBJECT': + simThAr_sci.append(archivo) + obname = h[0].header['OBJNAME'] + ra = h[0].header['OBJRA'] + delta = h[0].header['OBJDEC'] + airmass= h[0].header['AIRMASS'] + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'] + h[0].header['UTC-OBS'] + line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['EXPTYPE'], date, archivo) + f.write(line) + + elif h[0].header['EXPTYPE'] == 'BIAS': + biases.append(archivo) + mjd,mjd0 = mjd_fromheader(h) + bias_dates.append(mjd) + + elif h[0].header['EXPTYPE'] == 'FLAT': + flats.append(archivo) + mjd,mjd0 = mjd_fromheader(h) + flat_dates.append(mjd) + + elif h[0].header['EXPTYPE'] == 'COMPARISON': + mjd, mjd0 = mjd_fromheader(h) + ThAr_ref.append(archivo) + ThAr_ref_dates.append( mjd ) + + f.close() + biases, bias_dates = np.array(biases), np.array(bias_dates) + flats, flat_dates = np.array(flats), np.array(flat_dates) + IS = np.argsort(bias_dates) + biases, bias_dates = biases[IS], bias_dates[IS] + IS = np.argsort(flat_dates) + flats, flat_dates = flats[IS], flat_dates[IS] + + return biases, flats, ThAr_ref, simThAr_sci, ThAr_ref_dates def mjd_fromheader(h): """ return modified Julian date from header """ - + datetu = h[0].header['DATE-OBS'] timetu = h[0].header['UTC-OBS'].split(':') @@ -131,38 +132,38 @@ def mjd_fromheader(h): return mjd, mjd0 def MedianCombine(ImgList, zero_bo=False, zero='MasterBias.fits'): - """ - Median combine a list of images - """ - if zero_bo: - BIAS = pyfits.getdata(zero) - - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - - h = pyfits.open(ImgList[0])[0] - d = h.data - d = OverscanTrim(d) - if zero_bo: - d -= BIAS - d = np.round(d).astype('int') - factor = 1.25 - if (n < 3): - factor = 1 - ronoise = factor * h.header['RDNOISEA'] / np.sqrt(n) - gain = h.header['GAINA'] - if (n == 1): - return d, ronoise, gain - else: - for i in range(n-1): - h = pyfits.open(ImgList[i+1])[0] - ot = OverscanTrim(h.data) - if zero_bo: - d = np.dstack((d,np.round((ot - BIAS)).astype('int'))) - else: - d = np.dstack((d,np.round(ot).astype('int'))) - return np.median(d,axis=2), ronoise, gain + """ + Median combine a list of images + """ + if zero_bo: + BIAS = pyfits.getdata(zero) + + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + + h = pyfits.open(ImgList[0])[0] + d = h.data + d = OverscanTrim(d) + if zero_bo: + d -= BIAS + d = np.round(d).astype('int') + factor = 1.25 + if (n < 3): + factor = 1 + ronoise = factor * h.header['RDNOISEA'] / np.sqrt(n) + gain = h.header['GAINA'] + if (n == 1): + return d, ronoise, gain + else: + for i in range(n-1): + h = pyfits.open(ImgList[i+1])[0] + ot = OverscanTrim(h.data) + if zero_bo: + d = np.dstack((d,np.round((ot - BIAS)).astype('int'))) + else: + d = np.dstack((d,np.round(ot).astype('int'))) + return np.median(d,axis=2), ronoise, gain def OverscanTrim(d): """ @@ -176,316 +177,316 @@ def OverscanTrim(d): return newdata def get_them(sc,exap,ncoef,mode=1,shift=0.): - def gauss(params,x): - amp1 = params[0] - med1 = params[1] - sig1 = params[2] - g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) - return g1 - def res_gauss(params,g,x): - return g-gauss(params,x) - - def gauss2(params,x): - amp1 = params[0] - amp2 = params[1] - med1 = params[2] - med2 = params[3] - sig1 = params[4] - sig2 = params[5] - g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) - g2 = amp2 * np.exp(-0.5*((x-med2)/sig2)**2) - return g1 + g2 - - def res_gauss2(params,g,x): - return g-gauss2(params,x) - - def fit_drift(ref,dif,oref): - coef = np.polyfit(ref,dif,2) - - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - dif = np.delete(dif,im) - ref = np.delete(ref,im) - coef = np.polyfit(ref,dif,2) - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - - cdif = np.polyval(coef,oref) - ref = oref + cdif - return ref - - sc_or = sc.copy() - medc = int(.5*sc.shape[1]) - d = np.median(sc[:,medc-exap:medc+exap+1],axis=1) - #plot(d) - ejx = np.arange(len(d)) - - ref = [] - cents = np.loadtxt('ref_ords.txt').astype('int') - shift - #plot(d) - #plot(ejx[cents[:,0]],d[cents[:,0]],'ro') - #plot(ejx[cents[:,1]],d[cents[:,1]],'go') - #show() - - posO, posC = cents[:,0],cents[:,1] - if mode == 0: - posO = cents[:,2] - posC = cents[:,2] - refO,refC = [],[] - exap2 = exap + .2*exap - dev = exap2/4. - for i in range(len(posO)): - if posO[i]-exap2 < 0: - x = ejx[:posC[i]+exap2+1] - y = d[:posC[i]+exap2+1] - elif posC[i]+exap2+1 > len(d): - x = ejx[posO[i]-exap2:] - y = d[posO[i]-exap2:] - else: - x = ejx[posO[i]-exap2:posC[i]+exap2+1] - y = d[posO[i]-exap2:posC[i]+exap2+1] - y -= y.min() - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) - ty2 = np.zeros(len(tx2)) - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - - midi = int(0.5*len(x)) - - if mode == 1: - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - refO.append(p[2]) - refC.append(p[3]) - else: - #plot(x,y) - #show() - #print fcds - guess = [np.max(y),x[0]+np.argmax(y),dev] - p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) - refO.append(p[1]) - refC.append(p[1]) - - refO = np.array(refO) - refOt = np.around(refO).astype('int') - matO = np.zeros((len(refO),sc.shape[1])) - matO[:,medc] = refO - refC = np.array(refC) - refCt = np.around(refC).astype('int') - matC = np.zeros((len(refC),sc.shape[1])) - matC[:,medc] = refC - - #plot(d) - #plot(cents[:,0],d[cents[:,2]],'ro') - #plot(cents[:,1],d[cents[:,1]],'go') - #plot(refOt,d[refOt],'ko') - #plot(refCt,d[refCt],'yo') - #show() - #print gfd5 - i = medc -1 - while i >=0: - d = sc[:,i] - #if i<1000: - # plot(d) - j = 0 - posO = np.around(refO).astype('int') - posC = np.around(refC).astype('int') - trefO,trefC = [],[] - exap2 = exap + .2*exap - dev = exap2/4. - - while j < len(posO): - #print j - if posO[j]-exap2 < 0: - x = ejx[:posC[j]+exap2+1] - y = d[:posC[j]+exap2+1] - elif posC[j]+exap2+1 > len(d): - x = ejx[posO[j]-exap2:] - y = d[posO[j]-exap2:] - else: - x = ejx[posO[j]-exap2:posC[j]+exap2+1] - y = d[posO[j]-exap2:posC[j]+exap2+1] - if len(x) < 7: - trefO.append(refO[j]) - trefC.append(refC[j]) - else: - y -= y.min() - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) - ty2 = np.zeros(len(tx2)) - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - midi = int(0.5*len(x)) - if mode == 1: - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - trefO.append(p[2]) - trefC.append(p[3]) - else: - guess = [np.max(y),x[0]+np.argmax(y),dev] - p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) - trefO.append(p[1]) - trefC.append(p[1]) - - j+=1 - - orefO = refO.copy() - trefO = np.array(trefO) - difO = trefO-refO - refO = fit_drift(refO,difO,orefO) - matO[:,i] = refO - orefC = refC.copy() - trefC = np.array(trefC) - difC = trefC-refC - refC = fit_drift(refC,difC,orefC) - matC[:,i] = refC - - #if i<500: - # plot(d) - # I = np.where(refO<2049)[0] - # plot(refO[I].astype('int'),d[refO[I].astype('int')],'ko') - # show() - # print gfd - - i-=4 - - refO = matO[:,medc] - refC = matC[:,medc] - - i = medc +1 - while i < sc.shape[1]: - d = sc[:,i] - j = 0 - posO = np.around(refO).astype('int') - posC = np.around(refC).astype('int') - trefO,trefC = [],[] - exap2 = exap + .2*exap - dev = exap2/4. - - while j < len(posO): - #print j - if posO[j]-exap2 < 0: - x = ejx[:posC[j]+exap2+1] - y = d[:posC[j]+exap2+1] - elif posC[j]+exap2+1 > len(d): - x = ejx[posO[j]-exap2:] - y = d[posO[j]-exap2:] - else: - x = ejx[posO[j]-exap2:posC[j]+exap2+1] - y = d[posO[j]-exap2:posC[j]+exap2+1] - if len(x) < 7: - trefO.append(refO[j]) - trefC.append(refC[j]) - else: - y -= y.min() - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) - ty2 = np.zeros(len(tx2)) - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - midi = int(0.5*len(x)) - if mode == 1: - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - trefO.append(p[2]) - trefC.append(p[3]) - else: - guess = [np.max(y),x[0]+np.argmax(y),dev] - p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) - trefO.append(p[1]) - trefC.append(p[1]) - j+=1 - - orefO = refO.copy() - trefO = np.array(trefO) - difO = trefO-refO - refO = fit_drift(refO,difO,orefO) - matO[:,i] = refO - - orefC = refC.copy() - trefC = np.array(trefC) - difC = trefC-refC - refC = fit_drift(refC,difC,orefC) - matC[:,i] = refC - - i+=4 - - #imshow(sc_or,vmax=5000) - for i in range(matO.shape[0]): - y = matO[i] - x = np.arange(len(y)) - I = np.where(y!=0)[0] - x,y = x[I],y[I] - coef = np.polyfit(x,y,ncoef) - #plot(x,y,'r') - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - x = np.delete(x,im) - y = np.delete(y,im) - coef = np.polyfit(x,y,ncoef) - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - if i == 0: - acoefsO = coef - else: - acoefsO = np.vstack((acoefsO,coef)) - #plot(np.polyval(coef,np.arange(len(matO[i]))),'g') - #show() - - if mode == 1: - for i in range(matC.shape[0]): - y = matC[i] - x = np.arange(len(y)) - I = np.where(y!=0)[0] - x,y = x[I],y[I] - coef = np.polyfit(x,y,ncoef) - #plot(x,y,'r') - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - x = np.delete(x,im) - y = np.delete(y,im) - coef = np.polyfit(x,y,ncoef) - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - if i == 0: - acoefsC = coef - else: - acoefsC = np.vstack((acoefsC,coef)) - #plot(np.polyval(coef,np.arange(len(matC[i]))),'b') - #show() - #print gfds - #print acoefs.shape - return acoefsO, acoefsC - else: - return acoefsO \ No newline at end of file + def gauss(params,x): + amp1 = params[0] + med1 = params[1] + sig1 = params[2] + g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) + return g1 + def res_gauss(params,g,x): + return g-gauss(params,x) + + def gauss2(params,x): + amp1 = params[0] + amp2 = params[1] + med1 = params[2] + med2 = params[3] + sig1 = params[4] + sig2 = params[5] + g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) + g2 = amp2 * np.exp(-0.5*((x-med2)/sig2)**2) + return g1 + g2 + + def res_gauss2(params,g,x): + return g-gauss2(params,x) + + def fit_drift(ref,dif,oref): + coef = np.polyfit(ref,dif,2) + + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + dif = np.delete(dif,im) + ref = np.delete(ref,im) + coef = np.polyfit(ref,dif,2) + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + + cdif = np.polyval(coef,oref) + ref = oref + cdif + return ref + + sc_or = sc.copy() + medc = int(.5*sc.shape[1]) + d = np.median(sc[:,medc-exap:medc+exap+1],axis=1) + #plot(d) + ejx = np.arange(len(d)) + + ref = [] + cents = np.loadtxt('ref_ords.txt').astype('int') - shift + #plot(d) + #plot(ejx[cents[:,0]],d[cents[:,0]],'ro') + #plot(ejx[cents[:,1]],d[cents[:,1]],'go') + #show() + + posO, posC = cents[:,0],cents[:,1] + if mode == 0: + posO = cents[:,2] + posC = cents[:,2] + refO,refC = [],[] + exap2 = exap + .2*exap + dev = exap2/4. + for i in range(len(posO)): + if posO[i]-exap2 < 0: + x = ejx[:posC[i]+exap2+1] + y = d[:posC[i]+exap2+1] + elif posC[i]+exap2+1 > len(d): + x = ejx[posO[i]-exap2:] + y = d[posO[i]-exap2:] + else: + x = ejx[posO[i]-exap2:posC[i]+exap2+1] + y = d[posO[i]-exap2:posC[i]+exap2+1] + y -= y.min() + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + ty2 = np.zeros(len(tx2)) + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + + midi = int(0.5*len(x)) + + if mode == 1: + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + refO.append(p[2]) + refC.append(p[3]) + else: + #plot(x,y) + #show() + #print fcds + guess = [np.max(y),x[0]+np.argmax(y),dev] + p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) + refO.append(p[1]) + refC.append(p[1]) + + refO = np.array(refO) + refOt = np.around(refO).astype('int') + matO = np.zeros((len(refO),sc.shape[1])) + matO[:,medc] = refO + refC = np.array(refC) + refCt = np.around(refC).astype('int') + matC = np.zeros((len(refC),sc.shape[1])) + matC[:,medc] = refC + + #plot(d) + #plot(cents[:,0],d[cents[:,2]],'ro') + #plot(cents[:,1],d[cents[:,1]],'go') + #plot(refOt,d[refOt],'ko') + #plot(refCt,d[refCt],'yo') + #show() + #print gfd5 + i = medc -1 + while i >=0: + d = sc[:,i] + #if i<1000: + # plot(d) + j = 0 + posO = np.around(refO).astype('int') + posC = np.around(refC).astype('int') + trefO,trefC = [],[] + exap2 = exap + .2*exap + dev = exap2/4. + + while j < len(posO): + #print j + if posO[j]-exap2 < 0: + x = ejx[:posC[j]+exap2+1] + y = d[:posC[j]+exap2+1] + elif posC[j]+exap2+1 > len(d): + x = ejx[posO[j]-exap2:] + y = d[posO[j]-exap2:] + else: + x = ejx[posO[j]-exap2:posC[j]+exap2+1] + y = d[posO[j]-exap2:posC[j]+exap2+1] + if len(x) < 7: + trefO.append(refO[j]) + trefC.append(refC[j]) + else: + y -= y.min() + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + ty2 = np.zeros(len(tx2)) + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + midi = int(0.5*len(x)) + if mode == 1: + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + trefO.append(p[2]) + trefC.append(p[3]) + else: + guess = [np.max(y),x[0]+np.argmax(y),dev] + p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) + trefO.append(p[1]) + trefC.append(p[1]) + + j+=1 + + orefO = refO.copy() + trefO = np.array(trefO) + difO = trefO-refO + refO = fit_drift(refO,difO,orefO) + matO[:,i] = refO + orefC = refC.copy() + trefC = np.array(trefC) + difC = trefC-refC + refC = fit_drift(refC,difC,orefC) + matC[:,i] = refC + + #if i<500: + # plot(d) + # I = np.where(refO<2049)[0] + # plot(refO[I].astype('int'),d[refO[I].astype('int')],'ko') + # show() + # print gfd + + i-=4 + + refO = matO[:,medc] + refC = matC[:,medc] + + i = medc +1 + while i < sc.shape[1]: + d = sc[:,i] + j = 0 + posO = np.around(refO).astype('int') + posC = np.around(refC).astype('int') + trefO,trefC = [],[] + exap2 = exap + .2*exap + dev = exap2/4. + + while j < len(posO): + #print j + if posO[j]-exap2 < 0: + x = ejx[:posC[j]+exap2+1] + y = d[:posC[j]+exap2+1] + elif posC[j]+exap2+1 > len(d): + x = ejx[posO[j]-exap2:] + y = d[posO[j]-exap2:] + else: + x = ejx[posO[j]-exap2:posC[j]+exap2+1] + y = d[posO[j]-exap2:posC[j]+exap2+1] + if len(x) < 7: + trefO.append(refO[j]) + trefC.append(refC[j]) + else: + y -= y.min() + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + ty2 = np.zeros(len(tx2)) + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + midi = int(0.5*len(x)) + if mode == 1: + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + trefO.append(p[2]) + trefC.append(p[3]) + else: + guess = [np.max(y),x[0]+np.argmax(y),dev] + p, success = scipy.optimize.leastsq(res_gauss, guess, args=(y,x)) + trefO.append(p[1]) + trefC.append(p[1]) + j+=1 + + orefO = refO.copy() + trefO = np.array(trefO) + difO = trefO-refO + refO = fit_drift(refO,difO,orefO) + matO[:,i] = refO + + orefC = refC.copy() + trefC = np.array(trefC) + difC = trefC-refC + refC = fit_drift(refC,difC,orefC) + matC[:,i] = refC + + i+=4 + + #imshow(sc_or,vmax=5000) + for i in range(matO.shape[0]): + y = matO[i] + x = np.arange(len(y)) + I = np.where(y!=0)[0] + x,y = x[I],y[I] + coef = np.polyfit(x,y,ncoef) + #plot(x,y,'r') + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + x = np.delete(x,im) + y = np.delete(y,im) + coef = np.polyfit(x,y,ncoef) + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + if i == 0: + acoefsO = coef + else: + acoefsO = np.vstack((acoefsO,coef)) + #plot(np.polyval(coef,np.arange(len(matO[i]))),'g') + #show() + + if mode == 1: + for i in range(matC.shape[0]): + y = matC[i] + x = np.arange(len(y)) + I = np.where(y!=0)[0] + x,y = x[I],y[I] + coef = np.polyfit(x,y,ncoef) + #plot(x,y,'r') + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + x = np.delete(x,im) + y = np.delete(y,im) + coef = np.polyfit(x,y,ncoef) + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + if i == 0: + acoefsC = coef + else: + acoefsC = np.vstack((acoefsC,coef)) + #plot(np.polyval(coef,np.arange(len(matC[i]))),'b') + #show() + #print gfds + #print acoefs.shape + return acoefsO, acoefsC + else: + return acoefsO diff --git a/espadons/wav2.py b/espadons/wav2.py index bc050b2..a07e1da 100644 --- a/espadons/wav2.py +++ b/espadons/wav2.py @@ -1,3 +1,4 @@ +from __future__ import print_function import pyfits from pylab import * import scipy @@ -16,12 +17,12 @@ tlines = [] names = [] for line in lines: - cos = line.split() - tlines.append(float(cos[0])) - try: - names.append(cos[1]) - except: - names.append('?') + cos = line.split() + tlines.append(float(cos[0])) + try: + names.append(cos[1]) + except: + names.append('?') tlines = np.array(tlines) names = np.array(names) coefs = np.loadtxt('/Users/rabrahm/codes/ceres/espadons/wcal_ref.dat') @@ -31,15 +32,15 @@ nspec[5] = sc[:,1,:] for i in range(nspec.shape[1]): - npix = np.arange(nspec.shape[2]) - wav = np.polyval(coefs[i][1:][::-1],npix)*10 - nspec[0,i,:] = wav - spl = interpolate.splrep(np.arange(wav.shape[0]), wav,k=3) - dlambda_dx = interpolate.splev(np.arange(wav.shape[0]), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - nspec[9,i,:] = nspec[5,i,:] * (dlambda_dx ** 1) - nspec[8,i,:] = 100. + npix = np.arange(nspec.shape[2]) + wav = np.polyval(coefs[i][1:][::-1],npix)*10 + nspec[0,i,:] = wav + spl = interpolate.splrep(np.arange(wav.shape[0]), wav,k=3) + dlambda_dx = interpolate.splev(np.arange(wav.shape[0]), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + nspec[9,i,:] = nspec[5,i,:] * (dlambda_dx ** 1) + nspec[8,i,:] = 100. ml_v = tlines - 0.01 mh_v = tlines + 0.01 @@ -59,63 +60,59 @@ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) cent = np.sum(xc_av*vels) / np.sum(xc_av) -print cent +print(cent) #axvline(cent) #plot(vels,xc_av-j) #show() #print gfvcdxs for i in range(nspec.shape[1]): - si = str(i) - if i<10: - si = '0'+si - f = open('order_'+si+'.iwdat','w') - npix = np.arange(nspec.shape[2]) - wav = np.polyval(coefs[i][1:][::-1],npix)*10 - wav = wav*(1-cent/lux) - tck = interpolate.splrep(npix,wav,k=3) - wav = interpolate.splev(npix+np.polyval(scnd,npix),tck) - - I = np.where((tlines>wav[0]) & (tlines50)&(plnswav[0]) & (tlines50)&(plns Masterbias: done!" + print("\t\t-> Masterbias: done!") - # median combine list of flats + # median combine list of flats Flat, RO_flat, GA_flat = ferosutils.MedianCombine(flats, zero_bo=True, zero=dirout+'MasterBias.fits') # save this file for later reference hdu = pyfits.PrimaryHDU( Flat ) if (os.access(dirout+'Flat.fits',os.F_OK)): os.remove(dirout+'Flat.fits') hdu.writeto(dirout+'Flat.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") Flat = Flat.T - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all, nord_all = GLOBALutils.get_them(Flat, 5, trace_degree, maxords=-1,mode=2,startfrom=40,endat=1900, nsigmas=5) #print nord_all, len(c_all) @@ -178,11 +179,11 @@ I2 = np.arange(1,nord_all+1,2) c_ob, c_co = c_all[I1], c_all[I2] nord_ob, nord_co = len(I1), len(I2) - print "\t\t"+str(nord_ob)+" object orders found..." - print "\t\t"+str(nord_co)+" comparison orders found..." + print("\t\t"+str(nord_ob)+" object orders found...") + print("\t\t"+str(nord_co)+" comparison orders found...") trace_dict = {'c_ob':c_ob, 'c_co':c_co, 'c_all':c_all, \ - 'nord_ob':nord_ob, 'nord_co':nord_co, 'nord_all':nord_all,\ + 'nord_ob':nord_ob, 'nord_co':nord_co, 'nord_all':nord_all,\ 'GA_flat':GA_flat,'RO_flat':RO_flat} pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) @@ -217,11 +218,11 @@ if (os.access(dname,os.F_OK)): os.remove(dname) hdu.writeto(dname) - print 'Dark of', tim,' seconds done!' + print('Dark of', tim,' seconds done!') -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') P_ob_fits = dirout + 'P_ob.fits' P_co_fits = dirout + 'P_co.fits' @@ -236,87 +237,87 @@ if ( os.access(P_ob_fits,os.F_OK) == False ) or ( os.access(P_co_fits,os.F_OK) == False ) or \ ( os.access(S_flat_ob_fits,os.F_OK) == False ) or ( os.access(S_flat_co_fits,os.F_OK) == False ) or \ (force_flat_extract): - "\t\t\tComputing Background..." - """ - Centers = np.zeros((len(c_all),Flat.shape[1])) - for i in range(c_all.shape[0]): - Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) - bac = GLOBALutils.get_scat(Flat,Centers,span=10,typ='min') - """ - #plot(Flat[:,2000]) - #plot(bac[:,2000]) - #show() - #print gfd - #Flat -= bac - - P_ob = GLOBALutils.obtain_P(Flat,c_ob,ext_aperture,RO_flat,\ - GA_flat,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) - P_co = GLOBALutils.obtain_P(Flat,c_co,ext_aperture,RO_flat,\ - GA_flat,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) - P = P_ob + P_co - - if ( os.access(P_fits,os.F_OK) ): - os.remove(P_fits) - if (os.access(P_ob_fits,os.F_OK)): - os.remove( P_ob_fits ) - if (os.access(P_co_fits,os.F_OK)): - os.remove( P_co_fits ) - - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( P_fits ) - hdu = pyfits.PrimaryHDU( P_ob ) - hdu.writeto( P_ob_fits ) - hdu = pyfits.PrimaryHDU( P_co ) - hdu.writeto( P_co_fits ) - - print "\t\t\tNo extracted flat object spectra found or extraction forced, extracting and saving..." - - S_flat_ob = GLOBALutils.optimal_extraction(Flat,P_ob,c_ob,ext_aperture,\ - RO_flat,GA_flat,S_Marsh,NCosmic_Marsh,\ - min_extract_col,max_extract_col,npools) - - # write P_on and S_flat_ob as fits files - if (os.access(S_flat_ob_fits,os.F_OK)): - os.remove( S_flat_ob_fits ) - - hdu = pyfits.PrimaryHDU( S_flat_ob ) - hdu.writeto( S_flat_ob_fits ) - - print "\t\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving..." - S_flat_co = GLOBALutils.optimal_extraction(Flat,P_co,c_co,ext_aperture,RO_flat,GA_flat,\ - S_Marsh,NCosmic_Marsh,min_extract_col,\ - max_extract_col,npools) - - # write P_on and S_flat_co as fits files - - if (os.access(S_flat_co_fits,os.F_OK)): - os.remove( S_flat_co_fits ) - - hdu = pyfits.PrimaryHDU( S_flat_co ) - hdu.writeto( S_flat_co_fits ) - + "\t\t\tComputing Background..." + """ + Centers = np.zeros((len(c_all),Flat.shape[1])) + for i in range(c_all.shape[0]): + Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) + bac = GLOBALutils.get_scat(Flat,Centers,span=10,typ='min') + """ + #plot(Flat[:,2000]) + #plot(bac[:,2000]) + #show() + #print gfd + #Flat -= bac + + P_ob = GLOBALutils.obtain_P(Flat,c_ob,ext_aperture,RO_flat,\ + GA_flat,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) + P_co = GLOBALutils.obtain_P(Flat,c_co,ext_aperture,RO_flat,\ + GA_flat,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) + P = P_ob + P_co + + if ( os.access(P_fits,os.F_OK) ): + os.remove(P_fits) + if (os.access(P_ob_fits,os.F_OK)): + os.remove( P_ob_fits ) + if (os.access(P_co_fits,os.F_OK)): + os.remove( P_co_fits ) + + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( P_fits ) + hdu = pyfits.PrimaryHDU( P_ob ) + hdu.writeto( P_ob_fits ) + hdu = pyfits.PrimaryHDU( P_co ) + hdu.writeto( P_co_fits ) + + print("\t\t\tNo extracted flat object spectra found or extraction forced, extracting and saving...") + + S_flat_ob = GLOBALutils.optimal_extraction(Flat,P_ob,c_ob,ext_aperture,\ + RO_flat,GA_flat,S_Marsh,NCosmic_Marsh,\ + min_extract_col,max_extract_col,npools) + + # write P_on and S_flat_ob as fits files + if (os.access(S_flat_ob_fits,os.F_OK)): + os.remove( S_flat_ob_fits ) + + hdu = pyfits.PrimaryHDU( S_flat_ob ) + hdu.writeto( S_flat_ob_fits ) + + print("\t\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving...") + S_flat_co = GLOBALutils.optimal_extraction(Flat,P_co,c_co,ext_aperture,RO_flat,GA_flat,\ + S_Marsh,NCosmic_Marsh,min_extract_col,\ + max_extract_col,npools) + + # write P_on and S_flat_co as fits files + + if (os.access(S_flat_co_fits,os.F_OK)): + os.remove( S_flat_co_fits ) + + hdu = pyfits.PrimaryHDU( S_flat_co ) + hdu.writeto( S_flat_co_fits ) + else: - print "\t\tExtracted flat object spectra found, loading..." - print "\t\tExtracted flat comparison spectra found, loading..." - P_ob = pyfits.getdata( P_ob_fits ) - S_flat_ob = pyfits.getdata( S_flat_ob_fits ) - P_co = pyfits.getdata( P_co_fits ) - S_flat_co = pyfits.getdata( S_flat_co_fits ) + print("\t\tExtracted flat object spectra found, loading...") + print("\t\tExtracted flat comparison spectra found, loading...") + P_ob = pyfits.getdata( P_ob_fits ) + S_flat_ob = pyfits.getdata( S_flat_ob_fits ) + P_co = pyfits.getdata( P_co_fits ) + S_flat_co = pyfits.getdata( S_flat_co_fits ) # Normalize flat field spectra. S_flat_ob_n, norms_ob = GLOBALutils.FlatNormalize_single( S_flat_ob, mid=int(.5*S_flat_ob.shape[2])) S_flat_co_n, norms_co = GLOBALutils.FlatNormalize_single( S_flat_co, mid=int(.5*S_flat_co.shape[2])) if nord_ob < n_useful: - n_useful = thar_S_ob.shape[0] -print '\n\tExtraction of ThAr calibration frames:' + n_useful = thar_S_ob.shape[0] +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr+Ne files for fsim in ThAr_Ne_ref: - print "\t\tWorking on ThAr+Ne file ", fsim, "..." + print("\t\tWorking on ThAr+Ne file ", fsim, "...") hthar = pyfits.open( fsim ) dthar = ferosutils.OverscanTrim(pyfits.getdata(fsim)) @@ -332,20 +333,20 @@ thar_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' thar_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' thar_fits_co_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.co.fits.S' - + if ( os.access(thar_fits_ob,os.F_OK) == False ) or \ ( os.access(thar_fits_co,os.F_OK) == False ) or \ ( os.access(thar_fits_ob_simple,os.F_OK) == False ) or \ ( os.access(thar_fits_co_simple,os.F_OK) == False ) or \ (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") - thar_Ss_ob = GLOBALutils.simple_extraction(dthar,c_ob,ext_aperture,min_extract_col,max_extract_col,npools) - thar_S_ob = GLOBALutils.optimal_extraction(dthar,P_ob,c_ob,ext_aperture,RO_thar, GA_thar,S_Marsh,100.,min_extract_col,max_extract_col,npools) + thar_Ss_ob = GLOBALutils.simple_extraction(dthar,c_ob,ext_aperture,min_extract_col,max_extract_col,npools) + thar_S_ob = GLOBALutils.optimal_extraction(dthar,P_ob,c_ob,ext_aperture,RO_thar, GA_thar,S_Marsh,100.,min_extract_col,max_extract_col,npools) thar_Ss_co = GLOBALutils.simple_extraction(dthar,c_co,ext_aperture,min_extract_col,max_extract_col,npools) - thar_S_co = GLOBALutils.optimal_extraction(dthar,P_co,c_co,ext_aperture,RO_thar, GA_thar,S_Marsh,100.,min_extract_col,max_extract_col,npools) - + thar_S_co = GLOBALutils.optimal_extraction(dthar,P_co,c_co,ext_aperture,RO_thar, GA_thar,S_Marsh,100.,min_extract_col,max_extract_col,npools) + if (os.access(thar_fits_ob,os.F_OK)): os.remove( thar_fits_ob ) if (os.access(thar_fits_co,os.F_OK)): @@ -354,7 +355,7 @@ os.remove( thar_fits_ob_simple ) if (os.access(thar_fits_co_simple,os.F_OK)): os.remove( thar_fits_co_simple ) - + hdu = pyfits.PrimaryHDU( thar_S_ob ) hdu.writeto( thar_fits_ob ) hdu = pyfits.PrimaryHDU( thar_S_co ) @@ -364,19 +365,19 @@ hdu = pyfits.PrimaryHDU( thar_Ss_co ) hdu.writeto( thar_fits_co_simple ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") sorted_ThAr_Ne_dates = np.argsort( ThAr_Ne_ref_dates ) c_p2w,c_p2w_c = [],[] -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") for i in range(len(sorted_ThAr_Ne_dates)): - index = sorted_ThAr_Ne_dates[i] + index = sorted_ThAr_Ne_dates[i] hd = pyfits.getheader(ThAr_Ne_ref[index]) wavsol_pkl = dirout + ThAr_Ne_ref[index].split('/')[-1][:-4]+'wavsolpars.pkl' - + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tComputing wavelength solution of ThAr file", ThAr_Ne_ref[index] + print("\t\tComputing wavelength solution of ThAr file", ThAr_Ne_ref[index]) hthar = pyfits.open( ThAr_Ne_ref[index] ) mjd, mjd0 = ferosutils.mjd_fromheader( hthar ) @@ -390,29 +391,29 @@ iv_thar_ob = thar_S_ob[:,2,:] lines_thar_co = thar_S_co[:,1,:] iv_thar_co = thar_S_co[:,2,:] - + All_Pixel_Centers = np.array([]) All_Wavelengths = np.array([]) All_Orders = np.array([]) All_Centroids = np.array([]) All_Sigmas = np.array([]) All_Intensities = np.array([]) - All_residuals = np.array([]) + All_residuals = np.array([]) - if thar_S_ob.shape[0] < n_useful: - n_useful = thar_S_ob.shape[0] + if thar_S_ob.shape[0] < n_useful: + n_useful = thar_S_ob.shape[0] + + wavss =[] + orss = [] + order = o0 + mid_wav = [] + mid_col_wv = [] - wavss =[] - orss = [] - order = o0 - mid_wav = [] - mid_col_wv = [] - while order < o0 + n_useful: order_s = str(order+1) if (order < 9): order_s = '0'+str(order+1) - + thar_order_orig = lines_thar_ob[order,:] IV = iv_thar_ob[order,:] wei = np.sqrt( IV ) @@ -420,24 +421,24 @@ thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ - order_s+thar_end, thar_order, order, wei, rmsmax=100, \ - minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ - Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,del_width=4.0) - #coef2 = np.polyfit(pixel_centers, wavelengths,5) - #plot(pixel_centers,wavelengths-np.polyval(coef2,pixel_centers),'ro') - #axhline(0.) - #show() - mid_col_wv.append(GLOBALutils.Cheby_eval(coeffs_pix2wav, int(.5*len(thar_order)), len(thar_order))) - - c_p2w.append(coeffs_pix2wav) - mid_wav.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,int(.25*len(thar_order)), len(thar_order))) + rms_ms, residuals, centroids, sigmas, intensities =\ + GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ + order_s+thar_end, thar_order, order, wei, rmsmax=100, \ + minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ + Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,del_width=4.0) + #coef2 = np.polyfit(pixel_centers, wavelengths,5) + #plot(pixel_centers,wavelengths-np.polyval(coef2,pixel_centers),'ro') + #axhline(0.) + #show() + mid_col_wv.append(GLOBALutils.Cheby_eval(coeffs_pix2wav, int(.5*len(thar_order)), len(thar_order))) + + c_p2w.append(coeffs_pix2wav) + mid_wav.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,int(.25*len(thar_order)), len(thar_order))) wavss.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,int(len(thar_order)/2.), len(thar_order))) - orss.append(float(order)) - - if (order == 16): + orss.append(float(order)) + + if (order == 16): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(.5*len(thar_order)), len(thar_order) ) else: @@ -449,28 +450,28 @@ All_Centroids = np.append( All_Centroids, centroids) All_Sigmas = np.append( All_Sigmas, sigmas) All_Intensities = np.append( All_Intensities, intensities ) - All_residuals = np.append( All_residuals, residuals ) + All_residuals = np.append( All_residuals, residuals ) order+=1 - """ - refx = 1./(np.arange(len(mid_col_wv)).astype('float')+1) - mid_col_wv = np.array(mid_col_wv)#s[5:] - coef1 = np.polyfit(refx,mid_col_wv,6) - coef2 = np.polyfit(refx,mid_col_wv,7) - coef3 = np.polyfit(refx,mid_col_wv,8) - coef4 = np.polyfit(refx,mid_col_wv,9) - - plot(refx,mid_col_wv-np.polyval(coef1,refx),'o') - axhline(0.) - #show() - plot(refx,mid_col_wv-np.polyval(coef2,refx),'o') - axhline(0.) - #show() - plot(refx,mid_col_wv-np.polyval(coef3,refx),'o') - axhline(0.) - plot(refx,mid_col_wv-np.polyval(coef4,refx),'o') - axhline(0.) - show() - """ + """ + refx = 1./(np.arange(len(mid_col_wv)).astype('float')+1) + mid_col_wv = np.array(mid_col_wv)#s[5:] + coef1 = np.polyfit(refx,mid_col_wv,6) + coef2 = np.polyfit(refx,mid_col_wv,7) + coef3 = np.polyfit(refx,mid_col_wv,8) + coef4 = np.polyfit(refx,mid_col_wv,9) + + plot(refx,mid_col_wv-np.polyval(coef1,refx),'o') + axhline(0.) + #show() + plot(refx,mid_col_wv-np.polyval(coef2,refx),'o') + axhline(0.) + #show() + plot(refx,mid_col_wv-np.polyval(coef3,refx),'o') + axhline(0.) + plot(refx,mid_col_wv-np.polyval(coef4,refx),'o') + axhline(0.) + show() + """ p0 = np.zeros( npar_wsol ) p0[0] = (16+OO0) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ @@ -479,20 +480,20 @@ maxrms=MRMS, Inv=Inverse_m,minlines=1200,order0=OO0, \ ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - """ - ejxx = np.arange(4096) - for i in np.unique(G_ord): - I = np.where(G_ord == i)[0] - print i,len(I) - #m = G_ord+OO0 - #m2 = m[I][0]+np.zeros(len(ejxx)) - #chebs = ferosutils.Calculate_chebs(ejxx, m2, OO0, n_useful,4096,1,Inverse=Inverse_m) - #ret = (1.0/m2) * FEROSutils.Joint_Polynomial_Cheby(p1, chebs) - plot(G_wav[I],G_res[I],'.') - #plot(ejxx,ret,'k') - #plot(G_pix,G_wav,'ro') - show() - """ + """ + ejxx = np.arange(4096) + for i in np.unique(G_ord): + I = np.where(G_ord == i)[0] + print i,len(I) + #m = G_ord+OO0 + #m2 = m[I][0]+np.zeros(len(ejxx)) + #chebs = ferosutils.Calculate_chebs(ejxx, m2, OO0, n_useful,4096,1,Inverse=Inverse_m) + #ret = (1.0/m2) * FEROSutils.Joint_Polynomial_Cheby(p1, chebs) + plot(G_wav[I],G_res[I],'.') + #plot(ejxx,ret,'k') + #plot(G_pix,G_wav,'ro') + show() + """ # Now calibrate COMPARISON orders. Use p1 above as p0 All_Pixel_Centers_co = np.array([]) @@ -501,13 +502,13 @@ All_Centroids_co = np.array([]) All_Sigmas_co = np.array([]) All_Intensities_co = np.array([]) - All_residuals_co = np.array([]) + All_residuals_co = np.array([]) order = o0 while order < o0 + n_useful: order_s = str(order+1) if (order < 9): order_s = '0'+str(order+1) - + thar_order_orig = lines_thar_co[order,:] IV = iv_thar_co[order,:] wei = np.sqrt( IV ) @@ -515,11 +516,11 @@ thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ - order_s+thar_end, thar_order, order, wei, rmsmax=100, \ - minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ - Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,del_width=4.0) + rms_ms, residuals, centroids, sigmas, intensities =\ + GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ + order_s+thar_end, thar_order, order, wei, rmsmax=100, \ + minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ + Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,del_width=4.0) c_p2w_c.append(coeffs_pix2wav) All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) @@ -528,66 +529,66 @@ All_Centroids_co = np.append( All_Centroids_co, centroids) All_Sigmas_co = np.append( All_Sigmas_co, sigmas) All_Intensities_co = np.append( All_Intensities_co, intensities ) - All_residuals_co = np.append( All_residuals_co, residuals ) + All_residuals_co = np.append( All_residuals_co, residuals ) order+=1 - + p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ np.ones(All_Intensities_co.shape), p1, Cheby=use_cheby,\ maxrms=MRMS, Inv=Inverse_m,minlines=1200,order0=OO0, \ ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - #for io in range(int(G_ord_co.min()),int(G_ord_co.max()+1),1): - # I = np.where(G_ord_co == io)[0] - # tmpw,tmpr = G_wav_co[I],G_res_co[I] - # IJ = np.argsort(tmpw) - # tmpw,tmpr = tmpw[IJ],tmpr[IJ] - # plot(tmpw,tmpr,'.') - # plot(tmpw,scipy.signal.medfilt(tmpr,21),'.') - #axhline(0.) - #show() - - spec_ob,spec_co = np.zeros((2,n_useful,len(thar_order))),np.zeros((2,n_useful,len(thar_order))) - equis = np.arange( len(thar_order) ) - order = o0 - ind = 0 + #for io in range(int(G_ord_co.min()),int(G_ord_co.max()+1),1): + # I = np.where(G_ord_co == io)[0] + # tmpw,tmpr = G_wav_co[I],G_res_co[I] + # IJ = np.argsort(tmpw) + # tmpw,tmpr = tmpw[IJ],tmpr[IJ] + # plot(tmpw,tmpr,'.') + # plot(tmpw,scipy.signal.medfilt(tmpr,21),'.') + #axhline(0.) + #show() + + spec_ob,spec_co = np.zeros((2,n_useful,len(thar_order))),np.zeros((2,n_useful,len(thar_order))) + equis = np.arange( len(thar_order) ) + order = o0 + ind = 0 while order < n_useful+o0: - oss = order - o0 + oss = order - o0 m = order + OO0 - chebs = GLOBALutils.Calculate_chebs(equis, m, order0=OO0,\ - ntotal=n_useful, npix=len(equis), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol_ob = (1.0/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) - WavSol_co = (1.0/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(p1_co,chebs,ncoef_x,ncoef_m) - spec_ob[0,oss,:] = WavSol_ob + chebs = GLOBALutils.Calculate_chebs(equis, m, order0=OO0,\ + ntotal=n_useful, npix=len(equis), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol_ob = (1.0/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) + WavSol_co = (1.0/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(p1_co,chebs,ncoef_x,ncoef_m) + spec_ob[0,oss,:] = WavSol_ob spec_ob[1,oss,:] = lines_thar_ob[order] - spec_co[0,oss,:] = WavSol_co + spec_co[0,oss,:] = WavSol_co spec_co[1,oss,:] = lines_thar_co[order] - order+=1 + order+=1 - hdu_ob, hdu_co = pyfits.PrimaryHDU(spec_ob),pyfits.PrimaryHDU(spec_co) - if os.access(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits',os.F_OK): - os.system('rm ' + dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits') - hdu_ob.writeto(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits') - if os.access(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits',os.F_OK): - os.system('rm ' + dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits') - hdu_co.writeto(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits') + hdu_ob, hdu_co = pyfits.PrimaryHDU(spec_ob),pyfits.PrimaryHDU(spec_co) + if os.access(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits',os.F_OK): + os.system('rm ' + dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits') + hdu_ob.writeto(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_ob.fits') + if os.access(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits',os.F_OK): + os.system('rm ' + dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits') + hdu_co.writeto(dirout + ThAr_Ne_ref[index].split('/')[-1][:-5]+'_sp_co.fits') - # end COMPARISON orders. + # end COMPARISON orders. pdict = {'c_p2w':c_p2w,'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord,\ - 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,'G_res':G_res,\ - 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths,\ - 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers,\ - 'All_Sigmas':All_Sigmas, 'c_p2w_c':c_p2w_c,'p1_co':p1_co,\ - 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, 'G_wav_co':G_wav_co,\ - 'II_co':II_co, 'rms_ms_co':rms_ms_co, 'G_res_co':G_res_co,\ - 'All_Centroids_co':All_Centroids_co,'All_Wavelengths_co':All_Wavelengths_co,\ - 'All_Orders_co':All_Orders_co, 'All_Pixel_Centers_co':All_Pixel_Centers_co} + 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,'G_res':G_res,\ + 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths,\ + 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers,\ + 'All_Sigmas':All_Sigmas, 'c_p2w_c':c_p2w_c,'p1_co':p1_co,\ + 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, 'G_wav_co':G_wav_co,\ + 'II_co':II_co, 'rms_ms_co':rms_ms_co, 'G_res_co':G_res_co,\ + 'All_Centroids_co':All_Centroids_co,'All_Wavelengths_co':All_Wavelengths_co,\ + 'All_Orders_co':All_Orders_co, 'All_Pixel_Centers_co':All_Pixel_Centers_co} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl,'r')) #print gfd @@ -598,7 +599,7 @@ refidx = 0 fname = dirout +'thar_shifts.pdf' dct_shfts = {} - + force_shift = False if os.access(dirout+'shifts.pkl',os.F_OK): dct_shfts = pickle.load(open(dirout+'shifts.pkl','r')) @@ -610,7 +611,7 @@ else: force_shift = True #""" -print len(ThAr_all) +print(len(ThAr_all)) if force_shift and len(sorted_ThAr_Ne_dates)>6: f, axarr = plt.subplots(len(sorted_ThAr_Ne_dates), sharex=True,figsize=(5, 30)) Thar_shifts_out = dirout + 'ThAr_Ne_shifts.dat' @@ -632,32 +633,32 @@ fsim = ThAr_Ne_ref[sorted_ThAr_Ne_dates[i]] pdict = pickle.load( open(dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl','r' ) ) p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict['All_Pixel_Centers_co'],\ - pdict['All_Wavelengths_co'], pdict['All_Orders_co'],\ - np.ones(len(pdict['All_Wavelengths_co'])), refdct['p1_co'],\ - minlines=1200, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ - Cheby=use_cheby, Inv=Inverse_m, npix=Flat.shape[1],nx=ncoef_x,nm=ncoef_m) + GLOBALutils.Global_Wav_Solution_vel_shift(pdict['All_Pixel_Centers_co'],\ + pdict['All_Wavelengths_co'], pdict['All_Orders_co'],\ + np.ones(len(pdict['All_Wavelengths_co'])), refdct['p1_co'],\ + minlines=1200, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ + Cheby=use_cheby, Inv=Inverse_m, npix=Flat.shape[1],nx=ncoef_x,nm=ncoef_m) p_shifts.append(p_shift) p_mjds.append(pdict['mjd']) p_shift_ob, pix_centers_ob, orders_ob, wavelengths_ob, I_ob, rms_ms_ob, residuals_ob = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict['All_Pixel_Centers'],\ - pdict['All_Wavelengths'], pdict['All_Orders'],\ - np.ones(len(pdict['All_Wavelengths'])), refdct['p1'],\ - minlines=1200, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ - Cheby=use_cheby, Inv=Inverse_m, npix=Flat.shape[1],nx=ncoef_x,nm=ncoef_m) + GLOBALutils.Global_Wav_Solution_vel_shift(pdict['All_Pixel_Centers'],\ + pdict['All_Wavelengths'], pdict['All_Orders'],\ + np.ones(len(pdict['All_Wavelengths'])), refdct['p1'],\ + minlines=1200, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ + Cheby=use_cheby, Inv=Inverse_m, npix=Flat.shape[1],nx=ncoef_x,nm=ncoef_m) p_shifts_ob.append(p_shift_ob) i+=1 p_shifts = np.array(p_shifts) p_shifts_ob = np.array(p_shifts_ob) dif = np.absolute(np.mean(p_shifts-p_shifts_ob)) - vec_dif.append(dif) + vec_dif.append(dif) axarr[j].plot(p_shifts-p_shifts_ob) axarr[j].axhline(0) axarr[j].set_title(fref.split('/')[-1]+'offset: '+str(np.around(dif,5))) if dif < mindif: mindif = dif difs = j - print j, dif + print(j, dif) j+=1 dct_shfts['vals']=np.array(vec_dif) dct_shfts['names']=np.array(vec_nam) @@ -679,7 +680,7 @@ j+=1 else: refidx = 0 -print 'This:',ThAr_Ne_ref[sorted_ThAr_Ne_dates[refidx]] +print('This:',ThAr_Ne_ref[sorted_ThAr_Ne_dates[refidx]]) clf() #""" ### start of science frame reductions ### @@ -687,7 +688,7 @@ new_list_obnames = [] new_sky = [] new_sky_obnames = [] -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') for i in range(len(simThAr_sci)): fsim = simThAr_sci[i] hd = pyfits.getheader(fsim) @@ -696,12 +697,12 @@ if object2do.lower() == 'all': new_list.append(fsim) new_list_obnames.append( obname ) - print "\t\t"+obname + print("\t\t"+obname) else: if (obname.lower() == object2do.lower()): new_list.append(fsim) new_list_obnames.append( obname ) - print "\t\t"+obname + print("\t\t"+obname) for i in range(len(simSky_sci)): fsim = simSky_sci[i] @@ -711,12 +712,12 @@ if object2do.lower() == 'all': new_sky.append(fsim) new_sky_obnames.append( obname ) - print "\t\t"+obname + print("\t\t"+obname) else: if (obname.lower() == object2do.lower()): new_sky.append(fsim) new_sky_obnames.append( obname ) - print "\t\t"+obname + print("\t\t"+obname) if n_useful>nord_ob: n_useful=nord_ob @@ -746,19 +747,19 @@ for fsim in comp_list: h = pyfits.open(fsim) - print '\n' - print "\t--> Working on image: ", fsim + print('\n') + print("\t--> Working on image: ", fsim) mjd,mjd0 = ferosutils.mjd_fromheader(h) ronoise, gain = ferosutils.get_RG(pyfits.getheader(fsim)) - + # Object name obname = h[0].header['OBJECT'] observer = h[0].header['OBSERVER'] comp_type = h[0].header['HIERARCH ESO DPR TYPE'].split(',')[-1] - print "\t\tObject name:",obname - print "\t\tComparison fiber is:", comp_type + print("\t\tObject name:",obname) + print("\t\tComparison fiber is:", comp_type) # Open file, trim, overscan subtract and MasterBias subtract data = h[0].data @@ -791,10 +792,10 @@ ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 + ra = ra2 + dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') altitude = 2335. latitude = -29.2543 @@ -816,17 +817,17 @@ bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 #lbary_ltopo = bcvel_baryc / 2.99792458E5 + 1. - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name = 'Eso2.2' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) + gobs = ephem.Observer() + gobs.name = 'Eso2.2' + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) gobs.date = h[0].header['DATE-OBS'][:10] + ' ' + h[0].header['DATE-OBS'][11:] mephem = ephem.Moon() mephem.compute(gobs) @@ -836,8 +837,8 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel - + print('\t\tRadial Velocity of sacttered moonlight:',refvel) + ThAr_Ne_ref_m = ThAr_Ne_ref ThAr_Ne_ref_dates_m = ThAr_Ne_ref_dates @@ -850,7 +851,7 @@ if ( os.access(sci_fits_ob,os.F_OK) == False ) or ( os.access(sci_fits_co,os.F_OK) == False ) or \ ( os.access(sci_fits_ob_simple,os.F_OK) == False ) or ( os.access(sci_fits_co_simple,os.F_OK) == False ) or \ ( force_sci_extract ): - print "\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") sci_Ss_ob = GLOBALutils.simple_extraction(data,c_ob,ext_aperture,min_extract_col,max_extract_col,npools) sci_Ss_co = GLOBALutils.simple_extraction(data,c_co,ext_aperture,min_extract_col,max_extract_col,npools) apsnr = np.sqrt(np.median(sci_Ss_ob[18,1700:2100])) @@ -879,7 +880,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'][11:]) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -905,7 +906,7 @@ hdu = pyfits.PrimaryHDU( sci_Ss_co ) hdu.writeto( sci_fits_co_simple ) else: - print "\t\t"+fsim+"has already been extracted, reading in product fits files..." + print("\t\t"+fsim+"has already been extracted, reading in product fits files...") sci_S_ob = pyfits.getdata( sci_fits_ob ) sci_S_co = pyfits.getdata( sci_fits_co ) sci_Ss_ob = pyfits.getdata( sci_fits_ob_simple ) @@ -923,7 +924,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'][11:]) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (s)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -942,13 +943,13 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SMOONALT',str(mephem.alt)) # get ThAr closest in time - indice = sorted_ThAr_Ne_dates[refidx] + indice = sorted_ThAr_Ne_dates[refidx] hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR REF',ThAr_Ne_ref_m[indice].split('/')[-1][:-5]+'_sp_ob.fits') hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR REF CO',ThAr_Ne_ref_m[indice].split('/')[-1][:-5]+'_sp_co.fits') thar_fits_ob = dirout + ThAr_Ne_ref_m[indice].split('/')[-1][:-4]+'spec.ob.fits.S' thar_fits_co = dirout + ThAr_Ne_ref_m[indice].split('/')[-1][:-4]+'spec.co.fits.S' pkl_wsol = dirout + ThAr_Ne_ref_m[indice].split('/')[-1][:-4]+'wavsolpars.pkl' - print "\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + print("\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) if comp_type == 'WAVE': @@ -982,12 +983,12 @@ thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ - order_s+thar_end, thar_order, order, wei, rmsmax=100, \ - minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ - Dump_AllLines=True, Cheby=use_cheby,porder=5,del_width=4.0) - + rms_ms, residuals, centroids, sigmas, intensities =\ + GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ + order_s+thar_end, thar_order, order, wei, rmsmax=100, \ + minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ + Dump_AllLines=True, Cheby=use_cheby,porder=5,del_width=4.0) + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order ) @@ -997,33 +998,33 @@ order+=1 p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ np.ones(All_Intensities_co.shape), wsol_dict['p1_co'], Cheby=use_cheby,\ maxrms=MRMS, Inv=Inverse_m,minlines=1200,order0=OO0, \ ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(All_Pixel_Centers_co,\ - All_Wavelengths_co, All_Orders_co, np.ones(len(All_Wavelengths_co)), wsol_dict['p1_co'],\ - minlines=1000, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ - Cheby=use_cheby, Inv=Inverse_m, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - precision = rms_ms/np.sqrt(len(I)) + GLOBALutils.Global_Wav_Solution_vel_shift(All_Pixel_Centers_co,\ + All_Wavelengths_co, All_Orders_co, np.ones(len(All_Wavelengths_co)), wsol_dict['p1_co'],\ + minlines=1000, maxrms=MRMS,order0=OO0, ntotal=n_useful,\ + Cheby=use_cheby, Inv=Inverse_m, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + precision = rms_ms/np.sqrt(len(I)) good_quality = True if (precision > 5): good_quality = False - p_shift = 0. + p_shift = 0. p_shifts.append(p_shift) p_mjds.append(mjd) spec_co = np.zeros((2,n_useful,len(thar_order))) - equis = np.arange( len(thar_order) ) + equis = np.arange( len(thar_order) ) order = o0 while order < n_useful+o0: oss = order - o0 m = order + OO0 chebs = GLOBALutils.Calculate_chebs(equis, m, order0=OO0,\ - ntotal=n_useful, npix=len(equis), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + ntotal=n_useful, npix=len(equis), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) WavSol_co = (1.0/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(p1_co,chebs,ncoef_x,ncoef_m) spec_co[0,oss,:] = WavSol_co spec_co[1,oss,:] = lines_thar_co[order] @@ -1039,7 +1040,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH WAVSOL ERROR', precision, '[m/s]') # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) + equis = np.arange( data.shape[1] ) order = o0 temp_spec = np.zeros((n_useful,data.shape[1])) while order < n_useful+o0: @@ -1072,8 +1073,8 @@ L1 = np.where( fn == 0 )[0] L2 = np.where( fn2 == 0 )[0] L = np.unique(np.hstack([L1,L2])) - spec[3,oss,:] = spec[1,oss,:] / S_flat_ob_n[order,1,:] - sci_S_co[order,1] / S_flat_co_n[order,1,:] - spec[4,oss,:] = sci_S_ob[order,2,:] * ( S_flat_ob_n[order,1,:] ** 2 ) #OJO cambiar esto + spec[3,oss,:] = spec[1,oss,:] / S_flat_ob_n[order,1,:] - sci_S_co[order,1] / S_flat_co_n[order,1,:] + spec[4,oss,:] = sci_S_ob[order,2,:] * ( S_flat_ob_n[order,1,:] ** 2 ) #OJO cambiar esto spec[3,oss,L] = 0. spec[4,oss,L] = 0. order+=1 @@ -1086,9 +1087,9 @@ oss = order-o0 L = np.where( spec[1,oss] != 0 ) ccoef = GLOBALutils.get_cont_single(spec[0,oss],spec[3,oss],spec[4,oss],ll=1.5,lu=5,nc=nconts[oss]) - spec[5,oss,:][L] = spec[3,oss,L] / np.polyval(ccoef,spec[0,oss][L]) - #plot(spec[0,oss,:],spec[3,oss,:]) - #plot(spec[0,oss,:][L],np.polyval(ccoef,spec[0,oss][L])) + spec[5,oss,:][L] = spec[3,oss,L] / np.polyval(ccoef,spec[0,oss][L]) + #plot(spec[0,oss,:],spec[3,oss,:]) + #plot(spec[0,oss,:][L],np.polyval(ccoef,spec[0,oss][L])) nJ = np.where(np.isnan(spec[5,oss])==True)[0] nJ2 = np.where(np.isinf(spec[5,oss])==True)[0] spec[5,oss,nJ] = 1.0 @@ -1097,7 +1098,7 @@ spec[6,oss,:][L] = spec[4,oss,:][L] * (ratio ** 2 ) spec[7,oss,:][L] = ratio spec[8,oss,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + (ronoise/gain)**2 ) - #print spec[8,oss,1890:2010] + #print spec[8,oss,1890:2010] rI = np.where(spec[5,oss] > 1. + 8./spec[8,oss]) spec[5,oss,rI] = 1. rI = np.where(spec[5,oss] < - (1. + 8./spec[8,oss])) @@ -1107,10 +1108,10 @@ NN = np.average(dlambda_dx) dlambda_dx /= NN - spec[9,oss,:][L] = spec[5,oss,:][L] * (dlambda_dx[L] ** 1) + spec[9,oss,:][L] = spec[5,oss,:][L] * (dlambda_dx[L] ** 1) spec[10,oss,:][L] = spec[6,oss,:][L] / (dlambda_dx[L] ** 2) order+=1 - #show() + #show() if os.access(dirout + fout, os.F_OK): os.remove(dirout + fout) hdu.writeto(dirout + fout) @@ -1123,26 +1124,26 @@ #show() if len(p_mjds) > 3: - tck_shift = scipy.interpolate.splrep(p_mjds,p_shifts,k=1) + tck_shift = scipy.interpolate.splrep(p_mjds,p_shifts,k=1) elif len(p_mjds) > 1: - tck_shift = scipy.interpolate.splrep(p_mjds,p_shifts,k=1) + tck_shift = scipy.interpolate.splrep(p_mjds,p_shifts,k=1) for fsim in new_sky: - h = pyfits.open(fsim) - obname = h[0].header['OBJECT'] - mjd,mjd0 = ferosutils.mjd_fromheader(h) - if len(p_mjds) > 1: - p_shift = scipy.interpolate.splev(mjd,tck_shift) - else: - p_shift = 0. - fout = 'proc/' + obname + '_' + h[0].header['DATE-OBS'][:4] + h[0].header['DATE-OBS'][5:7] +\ - h[0].header['DATE-OBS'][8:10] + '_' +'UT' + h[0].header['DATE-OBS'][11:] + '_sp.fits' - hdu = pyfits.open(dirout + fout,mode='update') - hdu[0].data[0,:,:] *= (1.0 + 1.0e-6*p_shift) - hdu.flush() - hdu.close() - -print "\n\tSarting with the post-processing:" + h = pyfits.open(fsim) + obname = h[0].header['OBJECT'] + mjd,mjd0 = ferosutils.mjd_fromheader(h) + if len(p_mjds) > 1: + p_shift = scipy.interpolate.splev(mjd,tck_shift) + else: + p_shift = 0. + fout = 'proc/' + obname + '_' + h[0].header['DATE-OBS'][:4] + h[0].header['DATE-OBS'][5:7] +\ + h[0].header['DATE-OBS'][8:10] + '_' +'UT' + h[0].header['DATE-OBS'][11:] + '_sp.fits' + hdu = pyfits.open(dirout + fout,mode='update') + hdu[0].data[0,:,:] *= (1.0 + 1.0e-6*p_shift) + hdu.flush() + hdu.close() + +print("\n\tSarting with the post-processing:") #JustExtract = True if (not JustExtract): for fsim in comp_list: @@ -1161,7 +1162,7 @@ fout = 'proc/' + obname + '_' + h[0].header['DATE-OBS'][:4] + h[0].header['DATE-OBS'][5:7] +\ h[0].header['DATE-OBS'][8:10] + '_' +'UT' + h[0].header['DATE-OBS'][11:] + '_sp.fits' - print "\n\t--> Working on spectrum: ", fout + print("\n\t--> Working on spectrum: ", fout) hdu = pyfits.open(dirout + fout,mode='update') spec = hdu[0].data @@ -1178,19 +1179,19 @@ if DoClass: # spectral analysis - print "\t\tSpectral Analysis..." + print("\t\tSpectral Analysis...") query_success = False # First, query SIMBAD with the object name query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + fsim.split('/')[-1][:-4]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./50000**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -1202,10 +1203,10 @@ f.write(line) f.close() else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -1220,10 +1221,10 @@ hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VSINI', vsini) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") velw = 300 velsh = 3. # Read in mask @@ -1247,11 +1248,11 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' - boot_ind = np.arange(spec.shape[1]) - #boot_ind = np.arange(5,18,1) + print('\t\t\tComputing the CCF...') + boot_ind = np.arange(spec.shape[1]) + #boot_ind = np.arange(5,18,1) if True: cond = True while (cond): @@ -1271,7 +1272,7 @@ rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ @@ -1288,7 +1289,7 @@ moon_sig = 3.3 else: moon_sig = 4.5 - moon_sig = 0.6*np.sqrt(1.**2+disp**2) + moon_sig = 0.6*np.sqrt(1.**2+disp**2) p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) moonmatters = False @@ -1310,11 +1311,11 @@ #print p1gau[1] if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -1322,13 +1323,13 @@ BSerr = -999.00 xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m,'W_ccf':W_ccf} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m,'W_ccf':W_ccf} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) @@ -1337,11 +1338,11 @@ if not avoid_plot: GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - - airmass = h[0].header['ESO TEL AIRM START'] + + airmass = h[0].header['ESO TEL AIRM START'] seeing = h[0].header['ESO TEL AMBI FWHM START'] #moonsep = h[0].header['ESO TEL MOON DIST'] - TEXP = h[0].header['EXPTIME'] + TEXP = h[0].header['EXPTIME'] if sp_type=='G2': if T_eff < 6000: @@ -1380,17 +1381,17 @@ RVerr2 = RVerr * depth_fact if (RVerr2 <= 0.007): - RVerr2 = 0.007 + RVerr2 = 0.007 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) - BS2 = np.around(SP2,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) + BS2 = np.around(SP2,4) RVerr2 = np.around(RVerr2,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) - print '\t\t\tBS2 = '+str(BS2)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) + print('\t\t\tBS2 = '+str(BS2)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1418,8 +1419,8 @@ line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f feros ceres 50000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS2, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) + TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) hdu.close() -f_res.close() \ No newline at end of file +f_res.close() diff --git a/feros/ferosutils.py b/feros/ferosutils.py index 8257aff..a45b519 100644 --- a/feros/ferosutils.py +++ b/feros/ferosutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from astropy.io import fits as pyfits import numpy as np from numpy import median,sqrt,array,exp @@ -20,37 +21,37 @@ lowess = sm.nonparametric.lowess def gauss2(params,x): - amp1 = params[0] - amp2 = params[1] - med1 = params[2] - med2 = params[3] - sig1 = params[4] - sig2 = params[5] - g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) - g2 = amp2 * np.exp(-0.5*((x-med2)/sig2)**2) - return g1 + g2 + amp1 = params[0] + amp2 = params[1] + med1 = params[2] + med2 = params[3] + sig1 = params[4] + sig2 = params[5] + g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) + g2 = amp2 * np.exp(-0.5*((x-med2)/sig2)**2) + return g1 + g2 def res_gauss2(params,g,x): - return g-gauss2(params,x) + return g-gauss2(params,x) def get_RG(h): - if h['HIERARCH ESO DET READ MODE'] == 'normal': - ron = 5.1 - gain = 3.2 - elif h['HIERARCH ESO DET READ MODE'] == 'slow': - ron = 3.0 - gain = 1.0 - else: - ron = 7.2 - gain = 2.7 - return ron,gain + if h['HIERARCH ESO DET READ MODE'] == 'normal': + ron = 5.1 + gain = 3.2 + elif h['HIERARCH ESO DET READ MODE'] == 'slow': + ron = 3.0 + gain = 1.0 + else: + ron = 7.2 + gain = 2.7 + return ron,gain def MedianCombine(ImgList, zero_bo=False, zero='MasterBias.fits'): """ Median combine a list of images """ if zero_bo: - BIAS = pyfits.getdata(zero) + BIAS = pyfits.getdata(zero) n = len(ImgList) if n==0: @@ -61,7 +62,7 @@ def MedianCombine(ImgList, zero_bo=False, zero='MasterBias.fits'): d = OverscanTrim(d) d = b_col(d) if zero_bo: - d -= BIAS + d -= BIAS d = np.round(d).astype('int') factor = 1.25 @@ -77,14 +78,14 @@ def MedianCombine(ImgList, zero_bo=False, zero='MasterBias.fits'): return d, ronoise, gain else: for i in range(n-1): - #print i + #print i h = pyfits.open(ImgList[i+1])[0] ot = OverscanTrim(h.data) - ot = b_col(ot) - if zero_bo: - d = np.dstack((d,np.round((ot - BIAS)).astype('int'))) - else: - d = np.dstack((d,np.round(ot).astype('int'))) + ot = b_col(ot) + if zero_bo: + d = np.dstack((d,np.round((ot - BIAS)).astype('int'))) + else: + d = np.dstack((d,np.round(ot).astype('int'))) return np.median(d,axis=2), ronoise, gain def OverscanTrim(d): @@ -102,180 +103,180 @@ def OverscanTrim(d): return newdata def b_col(d): - - d2 = np.zeros(d.shape) - ps = [[1675.,4097., 219.],\ - [1675.,1780., 222.],\ - [ 0.,4097., 320.],\ - [ 0.,4097., 326.],\ - [1616.,1617., 334.],\ - [1617.,1641., 334.],\ - [1641.,1696., 335.],\ - [1696.,4097., 335.],\ - [1617.,1641., 338.],\ - [1622.,1741., 342.],\ - [1622.,1741., 343.],\ - [ 868.,4097., 646.],\ - [1514.,2101., 843.],\ - [1501.,1691., 857.],\ - [1501.,1691., 858.],\ - [1475.,1681., 883.],\ - [1459.,4097., 900.],\ - [1454.,1501., 916.],\ - [1404.,4907.,1062.],\ - [1404.,4907.,1063.],\ - [1404.,4907.,1064.],\ - [ 608.,4097.,1298.],\ - [ 0., 608.,1299.],\ - [ 608.,4097.,1299.]] - ps = np.array(ps).astype('int') - for i in range(len(ps)): - d2[ps[i][0]:ps[i][1],ps[i][2]] = 1 - ej = np.arange(d.shape[1]) - for i in range(d.shape[0]): - vec = d[i] - I = np.where(d2[i]==0)[0] - I2 = np.where(d2[i]==1)[0] - if len(I2)>0: - tck = scipy.interpolate.splrep(ej[I],vec[I],k=1) - d[i] = scipy.interpolate.splev(ej,tck) - - return d + + d2 = np.zeros(d.shape) + ps = [[1675.,4097., 219.],\ + [1675.,1780., 222.],\ + [ 0.,4097., 320.],\ + [ 0.,4097., 326.],\ + [1616.,1617., 334.],\ + [1617.,1641., 334.],\ + [1641.,1696., 335.],\ + [1696.,4097., 335.],\ + [1617.,1641., 338.],\ + [1622.,1741., 342.],\ + [1622.,1741., 343.],\ + [ 868.,4097., 646.],\ + [1514.,2101., 843.],\ + [1501.,1691., 857.],\ + [1501.,1691., 858.],\ + [1475.,1681., 883.],\ + [1459.,4097., 900.],\ + [1454.,1501., 916.],\ + [1404.,4907.,1062.],\ + [1404.,4907.,1063.],\ + [1404.,4907.,1064.],\ + [ 608.,4097.,1298.],\ + [ 0., 608.,1299.],\ + [ 608.,4097.,1299.]] + ps = np.array(ps).astype('int') + for i in range(len(ps)): + d2[ps[i][0]:ps[i][1],ps[i][2]] = 1 + ej = np.arange(d.shape[1]) + for i in range(d.shape[0]): + vec = d[i] + I = np.where(d2[i]==0)[0] + I2 = np.where(d2[i]==1)[0] + if len(I2)>0: + tck = scipy.interpolate.splrep(ej[I],vec[I],k=1) + d[i] = scipy.interpolate.splev(ej,tck) + + return d def gauss(params,x): - med = params[0] - sig = params[1] - g = np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) - return g + med = params[0] + sig = params[1] + g = np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) + return g def res_gauss(params,g,x): - return g-gauss(params,x) + return g-gauss(params,x) def FileClassify(diri, log, lamp='LAMP3'): - """ - Classifies all files in a directory and writes a night log of science images - """ - - # define output lists - simThAr_sci = [] - simSky_sci = [] - biases = [] - bias_dates = [] - flat_dates = [] - flats = [] - ThArNe_ref = [] - ThAr_Ne_ref = [] - ThArNe_ref_dates = [] - ThAr_Ne_ref_dates = [] - darks = [] - dark_times = [] - - f = open(log,'w') - bad_files = [] - if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - - all_files = glob.glob(diri+"*fits") - - jj = 0 - for archivo in all_files: - - jj+=1 - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - - if not dump: - h = pyfits.open(archivo) - print archivo, h[0].header['HIERARCH ESO DPR TYPE'] - - if h[0].header['HIERARCH ESO DPR TYPE'] == 'OBJECT,WAVE' or h[0].header['HIERARCH ESO DPR TYPE'] == 'VELOC,WAVE': - simThAr_sci.append(archivo) - obname = h[0].header['OBJECT'] - ra = h[0].header['HIERARCH ESO INS ADC1 RA'] - delta = h[0].header['HIERARCH ESO INS ADC1 DEC'] - try: - airmass= h[0].header['HIERARCH ESO TEL AIRM START'] - except: - airmass = -999. - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'] - line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['HIERARCH ESO DPR TYPE'], date, archivo) - f.write(line) - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'OBJECT,SKY' or h[0].header['HIERARCH ESO DPR TYPE'] == 'VELOC,SKY': - simSky_sci.append(archivo) - obname = h[0].header['OBJECT'] - ra = h[0].header['HIERARCH ESO INS ADC1 RA'] - delta = h[0].header['HIERARCH ESO INS ADC1 DEC'] - try: - airmass= h[0].header['HIERARCH ESO TEL AIRM START'] - except: - airmass = -999. - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'] - line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['HIERARCH ESO DPR TYPE'], date, archivo) - f.write(line) - - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'BIAS': - biases.append(archivo) - mjd,mjd0 = mjd_fromheader(h) - bias_dates.append(mjd) - - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'FLAT': - flats.append(archivo) - mjd,mjd0 = mjd_fromheader(h) - flat_dates.append(mjd) - - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'WAVE': - div = archivo.split('_') - if h[0].header['HIERARCH ESO INS CALMIRR2 ID'] == lamp: - ThArNe_ref.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - ThArNe_ref_dates.append( mjd ) - if h[0].header['HIERARCH ESO INS CALMIRR2 ID'] == lamp: - - ThAr_Ne_ref.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - ThAr_Ne_ref_dates.append( mjd ) - - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'DARK': - darks.append(archivo) - dark_times.append(h[0].header['EXPTIME']) - - f.close() - biases, bias_dates = np.array(biases), np.array(bias_dates) - flats, flat_dates = np.array(flats), np.array(flat_dates) - darks, dark_times = np.array(darks), np.array(dark_times) - IS = np.argsort(bias_dates) - biases, bias_dates = biases[IS], bias_dates[IS] - IS = np.argsort(flat_dates) - flats, flat_dates = flats[IS], flat_dates[IS] - - return biases, flats, ThArNe_ref, ThAr_Ne_ref, simThAr_sci, simSky_sci, ThArNe_ref_dates, ThAr_Ne_ref_dates, darks, dark_times + """ + Classifies all files in a directory and writes a night log of science images + """ + + # define output lists + simThAr_sci = [] + simSky_sci = [] + biases = [] + bias_dates = [] + flat_dates = [] + flats = [] + ThArNe_ref = [] + ThAr_Ne_ref = [] + ThArNe_ref_dates = [] + ThAr_Ne_ref_dates = [] + darks = [] + dark_times = [] + + f = open(log,'w') + bad_files = [] + if os.access(diri+'bad_files.txt',os.F_OK): + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + + all_files = glob.glob(diri+"*fits") + + jj = 0 + for archivo in all_files: + + jj+=1 + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + + if not dump: + h = pyfits.open(archivo) + print(archivo, h[0].header['HIERARCH ESO DPR TYPE']) + + if h[0].header['HIERARCH ESO DPR TYPE'] == 'OBJECT,WAVE' or h[0].header['HIERARCH ESO DPR TYPE'] == 'VELOC,WAVE': + simThAr_sci.append(archivo) + obname = h[0].header['OBJECT'] + ra = h[0].header['HIERARCH ESO INS ADC1 RA'] + delta = h[0].header['HIERARCH ESO INS ADC1 DEC'] + try: + airmass= h[0].header['HIERARCH ESO TEL AIRM START'] + except: + airmass = -999. + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'] + line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['HIERARCH ESO DPR TYPE'], date, archivo) + f.write(line) + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'OBJECT,SKY' or h[0].header['HIERARCH ESO DPR TYPE'] == 'VELOC,SKY': + simSky_sci.append(archivo) + obname = h[0].header['OBJECT'] + ra = h[0].header['HIERARCH ESO INS ADC1 RA'] + delta = h[0].header['HIERARCH ESO INS ADC1 DEC'] + try: + airmass= h[0].header['HIERARCH ESO TEL AIRM START'] + except: + airmass = -999. + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'] + line = "%-15s %10s %10s %8.2f %4.2f %s %8s %s\n" % (obname, ra, delta, texp, airmass, h[0].header['HIERARCH ESO DPR TYPE'], date, archivo) + f.write(line) + + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'BIAS': + biases.append(archivo) + mjd,mjd0 = mjd_fromheader(h) + bias_dates.append(mjd) + + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'FLAT': + flats.append(archivo) + mjd,mjd0 = mjd_fromheader(h) + flat_dates.append(mjd) + + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'WAVE': + div = archivo.split('_') + if h[0].header['HIERARCH ESO INS CALMIRR2 ID'] == lamp: + ThArNe_ref.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + ThArNe_ref_dates.append( mjd ) + if h[0].header['HIERARCH ESO INS CALMIRR2 ID'] == lamp: + + ThAr_Ne_ref.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + ThAr_Ne_ref_dates.append( mjd ) + + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'DARK': + darks.append(archivo) + dark_times.append(h[0].header['EXPTIME']) + + f.close() + biases, bias_dates = np.array(biases), np.array(bias_dates) + flats, flat_dates = np.array(flats), np.array(flat_dates) + darks, dark_times = np.array(darks), np.array(dark_times) + IS = np.argsort(bias_dates) + biases, bias_dates = biases[IS], bias_dates[IS] + IS = np.argsort(flat_dates) + flats, flat_dates = flats[IS], flat_dates[IS] + + return biases, flats, ThArNe_ref, ThAr_Ne_ref, simThAr_sci, simSky_sci, ThArNe_ref_dates, ThAr_Ne_ref_dates, darks, dark_times def mjd_fromheader(h): """ return modified Julian date from header """ - + datetu = h[0].header['DATE-OBS'] if len(datetu) < 23: - mjd_start = h[0].header['MJD-OBS'] - mjd0 = 2400000.5 + mjd_start = h[0].header['MJD-OBS'] + mjd0 = 2400000.5 else: - #print datetu - mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10])) - ho = int(datetu[11:13]) - mi = int(datetu[14:16]) - se = float(datetu[17:]) - ut = float(ho) + float(mi)/60.0 + float(se)/3600.0 - mjd_start = mjd + ut/24.0 + #print datetu + mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10])) + ho = int(datetu[11:13]) + mi = int(datetu[14:16]) + se = float(datetu[17:]) + ut = float(ho) + float(mi)/60.0 + float(se)/3600.0 + mjd_start = mjd + ut/24.0 secinday = 24*3600.0 fraction = 0.5 @@ -284,9 +285,9 @@ def mjd_fromheader(h): mjd = mjd_start + (fraction * texp) / secinday return mjd, mjd0 - + def Lines_mBack(thar, sd, thres_rel=3, pl=False): - """ + """ Given an extracted ThAr order, return a version where the background has been removed """ @@ -301,7 +302,7 @@ def Lines_mBack(thar, sd, thres_rel=3, pl=False): lines = FindLines_simple_sigma(d,sd, thres=thres_rel) # Now, mask these lines mask = np.ones( len(sd) ) - + for kk in lines: #mask region if (d[kk] > 10000): @@ -312,7 +313,7 @@ def Lines_mBack(thar, sd, thres_rel=3, pl=False): mask[kk-3:kk+4] = 0 else: mask[kk-3:kk+4] = 0 - + # New, final background estimnate X = np.array( range( len( d ) ) ) K = np.where((sd > 0) & (mask > 0)) @@ -328,47 +329,47 @@ def FindLines_simple_sigma(d,sd,thres=3): """ Given an array, find lines above a sigma-threshold """ - L = np.where( (d > (thres*sd)) & (d > 0) ) + L = np.where( (d > (thres*sd)) & (d > 0) ) lines = [] for i in range(np.shape(L)[1]): j = L[0][i] if ((d[j] > d[j-1]) & (d[j-1] > d[j-2]) & (d[j] > d[j+1]) & (d[j+1] > d[j+2])): lines.append(j) - + return lines ### wavelength calibration routines ### def sigma_clip(vec,lim=3.0): - while True: - med = np.median(vec) - res = vec - med - rms = np.sqrt(np.var(res)) - I = np.where(np.absolute(res) < lim*rms)[0] - if len(I) == len(vec): - break - else: - vec = vec[I] - return vec + while True: + med = np.median(vec) + res = vec - med + rms = np.sqrt(np.var(res)) + I = np.where(np.absolute(res) < lim*rms)[0] + if len(I) == len(vec): + break + else: + vec = vec[I] + return vec def get_dark(time,dnames,dtimes): - print dnames - print dtimes - print time - if len(dnames) == 0: - return 0. - elif len(dnames) == 1: - darko = pyfits.getdata(dnames[0]) - dark = darko * float(time)/float(dtimes[0]) - print np.median(darko),np.median(dark) - return darko - elif len(dnames) == 2: - sc0 = pyfits.getdata(dnames[0]).astype('float') - sc1 = pyfits.getdata(dnames[1]).astype('float') - t0,t1 = float(dtimes[0]),float(dtimes[1]) - m = (sc1 - sc0) / (t1 - t0) - n = sc1 - m*t1 - darko = m*float(time) + n - print np.median(sc0),np.median(sc1) - print np.median(sc1*time/dtimes[1]),np.median(sc0*time/dtimes[0]) - print np.median(darko) - return darko \ No newline at end of file + print(dnames) + print(dtimes) + print(time) + if len(dnames) == 0: + return 0. + elif len(dnames) == 1: + darko = pyfits.getdata(dnames[0]) + dark = darko * float(time)/float(dtimes[0]) + print(np.median(darko),np.median(dark)) + return darko + elif len(dnames) == 2: + sc0 = pyfits.getdata(dnames[0]).astype('float') + sc1 = pyfits.getdata(dnames[1]).astype('float') + t0,t1 = float(dtimes[0]),float(dtimes[1]) + m = (sc1 - sc0) / (t1 - t0) + n = sc1 - m*t1 + darko = m*float(time) + n + print(np.median(sc0),np.median(sc1)) + print(np.median(sc1*time/dtimes[1]),np.median(sc0*time/dtimes[0])) + print(np.median(darko)) + return darko diff --git a/feros/group.py b/feros/group.py index 9e51e14..d1f0922 100644 --- a/feros/group.py +++ b/feros/group.py @@ -1,37 +1,38 @@ +from __future__ import print_function import os from pylab import * f = open('/data/echelle/feros/k2_list.txt','r') lines = f.readlines() rv,rve,jd,refs = [],[],[],[] for line in lines: - path = line[:-1] - #line = 'python ferospipe.py '+path+' -npools 16 -do_class -avoid_plot -o2do HD157347 -reffile /data/echelle/feros/reffile.txt' - line = 'python ferospipe.py '+path+' -npools 16 -do_class' - os.system(line) - """ - pr = path[:-1]+'_red/proc/results.txt' + path = line[:-1] + #line = 'python ferospipe.py '+path+' -npools 16 -do_class -avoid_plot -o2do HD157347 -reffile /data/echelle/feros/reffile.txt' + line = 'python ferospipe.py '+path+' -npools 16 -do_class' + os.system(line) + """ + pr = path[:-1]+'_red/proc/results.txt' + + try: + fo = open(pr,'r') + lines2 = fo.readlines() + for line2 in lines2: + cos = line2.split() + if float(cos[16])>50 and cos[0]=='HD157347': + print line2[:-1] + rv.append(float(cos[2])) + rve.append(float(cos[3])) + jd.append(float(cos[1])) + st = path[:-1] + st = st.split('/')[-1] + mo = float(st[4:6]) + da = float(st[6:]) + ref = mo + da/30.5 + refs.append(ref) + except: + print 'bad' + + - try: - fo = open(pr,'r') - lines2 = fo.readlines() - for line2 in lines2: - cos = line2.split() - if float(cos[16])>50 and cos[0]=='HD157347': - print line2[:-1] - rv.append(float(cos[2])) - rve.append(float(cos[3])) - jd.append(float(cos[1])) - st = path[:-1] - st = st.split('/')[-1] - mo = float(st[4:6]) - da = float(st[6:]) - ref = mo + da/30.5 - refs.append(ref) - except: - print 'bad' - - - jd,rv,refs = np.array(jd),np.array(rv),np.array(refs) I = np.where(refs<20)[0] print np.sqrt(np.var(rv[I])) diff --git a/feros/run.py b/feros/run.py index 7fb15f0..1181415 100644 --- a/feros/run.py +++ b/feros/run.py @@ -1,8 +1,9 @@ +from __future__ import print_function import os f = open('/home/rabrahm/hd72673.list','r') lines = f.readlines() for line in lines: - night = line.split()[0] - os.system('python ferospipe.py ' + night + ' -npools 10 -do_class -o2do HD72673 -avoid_plot') + night = line.split()[0] + os.system('python ferospipe.py ' + night + ' -npools 10 -do_class -o2do HD72673 -avoid_plot') diff --git a/fideos/fideospipe.py b/fideos/fideospipe.py index 5adacda..d03ea95 100644 --- a/fideos/fideospipe.py +++ b/fideos/fideospipe.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use('Agg') @@ -58,7 +59,7 @@ avoid_plot = args.avoid_plot dirout = args.dirout DoClass = args.do_class -DoFull = args.do_full +DoFull = args.do_full JustExtract = args.just_extract npools = int(args.npools) object2do = args.o2do @@ -116,13 +117,13 @@ NSigma_Marsh = 10 NCosmic_Marsh = 10 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 0 max_extract_col = 2048 n_useful = 70 # up to which order do we care? order_dir = "wavcals/" -models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models +models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models ronoise, gain = 6.82, 1.9 @@ -133,10 +134,10 @@ just_thar = args.just_thar if just_bias or just_flats: force_pre_process = True -print "\n\n\tFIDEOS ESO1.0m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tFIDEOS ESO1.0m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') """ if os.access(dirin + 'log.txt', os.F_OK) and 1==2: @@ -184,10 +185,10 @@ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found, or pre-process forced..." + print("\tNo previous pre-processing files or found, or pre-process forced...") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 trace_file = dirout+'trace.pkl' @@ -195,70 +196,70 @@ if just_flats == False: MasterBias = GLOBALutils.MedianCombine_simple(biases,ZF=0.) - mbdate = str(datetime.datetime.now()).replace(' ','T') + mbdate = str(datetime.datetime.now()).replace(' ','T') if len(biases)>0: ronoise = np.around(fideosutils.compute_RON(MasterBias,biases,gain=gain),1) - print '\n\t\tRON:', ronoise,'e' + print('\n\t\tRON:', ronoise,'e') hdu = pyfits.PrimaryHDU( MasterBias ) - hdu = GLOBALutils.update_header(hdu,'RON',ronoise) - hdu = GLOBALutils.update_header(hdu,'DATE',mbdate) + hdu = GLOBALutils.update_header(hdu,'RON',ronoise) + hdu = GLOBALutils.update_header(hdu,'DATE',mbdate) hdu = fideosutils.fill_list_header(hdu,biases) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") MasterBias = pyfits.open(dirout+'MasterBias.fits') ronoise = MasterBias[0].header['RON'] if just_bias == False: - MasterFlat = GLOBALutils.MedianCombine_simple(flats,ZF=MasterBias[0].data) - mfdate = str(datetime.datetime.now()).replace(' ','T') - MasterFlat = np.fliplr(MasterFlat.T) - hdu1 = pyfits.PrimaryHDU( MasterFlat ) - hdu1 = GLOBALutils.update_header(hdu1,'BDATE',MasterBias[0].header['DATE']) - hdu1 = GLOBALutils.update_header(hdu1,'DATE',mfdate) - hdu1 = fideosutils.fill_list_header(hdu1,flats) - if (os.access(dirout+'MasterFlat.fits',os.F_OK)): - os.remove(dirout+'MasterFlat.fits') - hdu1.writeto(dirout+'MasterFlat.fits') - - MasterFlat_co = GLOBALutils.MedianCombine_simple(flats_co,ZF=MasterBias[0].data) - mfdate = str(datetime.datetime.now()).replace(' ','T') - MasterFlat_co = np.fliplr(MasterFlat_co.T) - hdu = pyfits.PrimaryHDU( MasterFlat_co ) - hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) - hdu = GLOBALutils.update_header(hdu,'DATE',mfdate) - hdu = fideosutils.fill_list_header(hdu,flats_co) - if (os.access(dirout+'MasterFlat_co.fits',os.F_OK)): - os.remove(dirout+'MasterFlat_co.fits') - hdu.writeto(dirout+'MasterFlat_co.fits') - - MasterFlat_ob = fideosutils.get_flatOB(MasterFlat,MasterFlat_co) - mfdate = str(datetime.datetime.now()).replace(' ','T') - hdu = pyfits.PrimaryHDU( MasterFlat_ob ) - hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) - hdu = GLOBALutils.update_header(hdu,'DATE',mfdate) - hdu = fideosutils.fill_list_header(hdu,flats) - if (os.access(dirout+'MasterFlat_ob.fits',os.F_OK)): - os.remove(dirout+'MasterFlat_ob.fits') - hdu.writeto(dirout+'MasterFlat_ob.fits') - print "\t\t-> Masterflats: done!" - - print "\t\t-> Tracing orders..." - c_co, nord_co = GLOBALutils.get_them(MasterFlat_co.T, 4.,trace_degree,maxords=-1,nsigmas=5,mode=1) - c_co, nord_co = fideosutils.good_orders(c_co,nord_co,MasterFlat.shape[1],MasterFlat.shape[0],ext_aperture_co) - print "\t\t-> Comparison orders found:", nord_co - temp_flat = fideosutils.make_flatOB(MasterFlat, c_co) - for i in range(temp_flat.shape[1]): - temp_flat[:,i] = scipy.ndimage.filters.gaussian_filter(temp_flat[:,i],3.) - c_ob, nord_ob = GLOBALutils.get_them(temp_flat, 7,trace_degree,maxords=-1,nsigmas=2.,mode=1) - c_ob, nord_ob = fideosutils.good_orders(c_ob,nord_ob,MasterFlat.shape[1],MasterFlat.shape[0],ext_aperture_ob) - - print "\t\t-> Object orders found:", nord_ob - - trace_dict = {'c_ob':c_ob,'nord_ob':nord_ob,'c_co':c_co,'nord_co':nord_co,'bdate':hdu1.header['BDATE'],'fdate':hdu1.header['DATE']} - pickle.dump( trace_dict, open( trace_file, 'w' ) ) + MasterFlat = GLOBALutils.MedianCombine_simple(flats,ZF=MasterBias[0].data) + mfdate = str(datetime.datetime.now()).replace(' ','T') + MasterFlat = np.fliplr(MasterFlat.T) + hdu1 = pyfits.PrimaryHDU( MasterFlat ) + hdu1 = GLOBALutils.update_header(hdu1,'BDATE',MasterBias[0].header['DATE']) + hdu1 = GLOBALutils.update_header(hdu1,'DATE',mfdate) + hdu1 = fideosutils.fill_list_header(hdu1,flats) + if (os.access(dirout+'MasterFlat.fits',os.F_OK)): + os.remove(dirout+'MasterFlat.fits') + hdu1.writeto(dirout+'MasterFlat.fits') + + MasterFlat_co = GLOBALutils.MedianCombine_simple(flats_co,ZF=MasterBias[0].data) + mfdate = str(datetime.datetime.now()).replace(' ','T') + MasterFlat_co = np.fliplr(MasterFlat_co.T) + hdu = pyfits.PrimaryHDU( MasterFlat_co ) + hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) + hdu = GLOBALutils.update_header(hdu,'DATE',mfdate) + hdu = fideosutils.fill_list_header(hdu,flats_co) + if (os.access(dirout+'MasterFlat_co.fits',os.F_OK)): + os.remove(dirout+'MasterFlat_co.fits') + hdu.writeto(dirout+'MasterFlat_co.fits') + + MasterFlat_ob = fideosutils.get_flatOB(MasterFlat,MasterFlat_co) + mfdate = str(datetime.datetime.now()).replace(' ','T') + hdu = pyfits.PrimaryHDU( MasterFlat_ob ) + hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) + hdu = GLOBALutils.update_header(hdu,'DATE',mfdate) + hdu = fideosutils.fill_list_header(hdu,flats) + if (os.access(dirout+'MasterFlat_ob.fits',os.F_OK)): + os.remove(dirout+'MasterFlat_ob.fits') + hdu.writeto(dirout+'MasterFlat_ob.fits') + print("\t\t-> Masterflats: done!") + + print("\t\t-> Tracing orders...") + c_co, nord_co = GLOBALutils.get_them(MasterFlat_co.T, 4.,trace_degree,maxords=-1,nsigmas=5,mode=1) + c_co, nord_co = fideosutils.good_orders(c_co,nord_co,MasterFlat.shape[1],MasterFlat.shape[0],ext_aperture_co) + print("\t\t-> Comparison orders found:", nord_co) + temp_flat = fideosutils.make_flatOB(MasterFlat, c_co) + for i in range(temp_flat.shape[1]): + temp_flat[:,i] = scipy.ndimage.filters.gaussian_filter(temp_flat[:,i],3.) + c_ob, nord_ob = GLOBALutils.get_them(temp_flat, 7,trace_degree,maxords=-1,nsigmas=2.,mode=1) + c_ob, nord_ob = fideosutils.good_orders(c_ob,nord_ob,MasterFlat.shape[1],MasterFlat.shape[0],ext_aperture_ob) + + print("\t\t-> Object orders found:", nord_ob) + + trace_dict = {'c_ob':c_ob,'nord_ob':nord_ob,'c_co':c_co,'nord_co':nord_co,'bdate':hdu1.header['BDATE'],'fdate':hdu1.header['DATE']} + pickle.dump( trace_dict, open( trace_file, 'w' ) ) trace_dict = pickle.load( open( trace_file, 'r' ) ) @@ -300,7 +301,7 @@ ( os.access(S_flat_co_fits,os.F_OK) == False ) or ( os.access(S_flat_co_simple_fits,os.F_OK) == False ) or \ (force_flat_extract) or just_flats: - print '\n\t Extracting Flats...' + print('\n\t Extracting Flats...') ejx = np.arange(MasterFlat[0].data.shape[0]) Centers = np.zeros((nord_ob,MasterFlat[0].data.shape[0])) @@ -315,7 +316,7 @@ hdu = pyfits.PrimaryHDU(bac_ob) if os.access(dirout +'BKG_flat_ob.fits',os.F_OK): - os.system('rm '+ dirout +'BKG_flat_ob.fits') + os.system('rm '+ dirout +'BKG_flat_ob.fits') hdu.writeto(dirout +'BKG_flat_ob.fits') Flat_ob = MasterFlat_ob[0].data.T - bac_ob @@ -387,7 +388,7 @@ hdu = pyfits.PrimaryHDU( S_flat_co_simple ) hdu.writeto( S_flat_co_simple_fits ) -print "\tExtracted flat comparison spectra found, loading...\n" +print("\tExtracted flat comparison spectra found, loading...\n") P_ob = pyfits.getdata( P_ob_fits ) S_flat_ob = pyfits.open( S_flat_ob_fits ) S_flat_ob_simple = pyfits.getdata( S_flat_ob_simple_fits ) @@ -402,7 +403,7 @@ = GLOBALutils.FlatNormalize( S_flat_co[0].data, S_flat_co_simple) if just_flats: - sys.exit() + sys.exit() reffiles = [] for i in range(nord_ob): @@ -413,7 +414,7 @@ #thars = thars[:3] thar_times = [] for fsim in thars: - print fsim + print(fsim) hthar = pyfits.open( fsim ) dthar = pyfits.getdata( fsim ) - MasterBias[0].data dthar = np.fliplr(dthar.T) @@ -425,7 +426,7 @@ thar_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' bac_fits_thar = dirout + 'BKG_'+ fsim.split('/')[-1][:-4]+'.fits' if ( os.access(thar_fits_ob_simple,os.F_OK) == False ) or ( os.access(thar_fits_co_simple,os.F_OK) == False ) or (force_thar_extract) or (just_thar): - print "No previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("No previous extraction or extraction forced for ThAr file", fsim, "extracting...") bac = fideosutils.get_scat(dthar.astype('float'), P_ob.astype('float') + P_co.astype('float'),1) dthar -= bac thar_Ss_ob_simple = GLOBALutils.simple_extraction(dthar.T,c_ob,ext_aperture_ob,min_extract_col,\ @@ -438,7 +439,7 @@ thar_Ss_co = GLOBALutils.optimal_extraction(dthar.T,P_co,c_co,ext_aperture_co,ronoise,gain,S_Marsh,\ NCosmic_Marsh,min_extract_col,max_extract_col,npools) thdate_co = str(datetime.datetime.now()).replace(' ','T') - + thar_Ss_ob_simple = GLOBALutils.invert(thar_Ss_ob_simple) thar_Ss_co_simple = GLOBALutils.invert(thar_Ss_co_simple) thar_Ss_ob = GLOBALutils.invert(thar_Ss_ob) @@ -481,7 +482,7 @@ thar_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' bac_fits_thar = dirout + 'BKG_'+ fsim.split('/')[-1][:-4]+'.fits' if ( os.access(thar_fits_co_simple,os.F_OK) == False ) or (force_thar_extract) or (just_thar): - print "No previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("No previous extraction or extraction forced for ThAr file", fsim, "extracting...") bac = fideosutils.get_scat(dthar.astype('float'), P_co.astype('float'),1) dthar -= bac thar_Ss_co_simple = GLOBALutils.simple_extraction(dthar.T,c_co,ext_aperture_co,min_extract_col,\ @@ -489,7 +490,7 @@ thar_Ss_co = GLOBALutils.optimal_extraction(dthar.T,P_co,c_co,ext_aperture_co,ronoise,gain,S_Marsh,\ NCosmic_Marsh,min_extract_col,max_extract_col,npools) thdate_co = str(datetime.datetime.now()).replace(' ','T') - + thar_Ss_co_simple = GLOBALutils.invert(thar_Ss_co_simple) thar_Ss_co = GLOBALutils.invert(thar_Ss_co) @@ -509,39 +510,39 @@ thars_co = thars_co[np.argsort(thar_co_times)] thar_co_times = thar_co_times[np.argsort(thar_co_times)] -print '\tSelection of echelle orders...' -print '\tUsing image:', thars[0] +print('\tSelection of echelle orders...') +print('\tUsing image:', thars[0]) force_thar_id = False if os.access(dirout+'order_id.pkl', os.F_OK) == False or force_thar_id: start_ob = 0 end_ob = 0 offset_ob = 0 if not just_comp: - print '\t\tComputing CCF...' + print('\t\tComputing CCF...') thar_fits_ob = dirout + thars[0].split('/')[-1][:-4]+'spec.ob.fits.S' thar_ob = pyfits.getdata(thar_fits_ob)[:,1,:] or20_ob, offset_ob = GLOBALutils.identify_order(thar_ob,order_dir+'fideos_20.iwdat',window=100,di=0.5) start_ob = or20_ob - 9 end_ob = start_ob + 38 start_ob_MgIII = or20_ob - 20 - end_ob_MgIII = start_ob_MgIII + 49 - print '\t\tWill start on order', start_ob, 'for object fibre...' - print '\t\tOffset for object fibre is ', offset_ob, 'pixels' + end_ob_MgIII = start_ob_MgIII + 49 + print('\t\tWill start on order', start_ob, 'for object fibre...') + print('\t\tOffset for object fibre is ', offset_ob, 'pixels') - print '\tUsing image:', thars[0] + print('\tUsing image:', thars[0]) thar_fits_co = dirout + thars[0].split('/')[-1][:-4]+'spec.co.fits.S' thar_co = pyfits.getdata(thar_fits_co)[:,1,:] or20_co, offset_co = GLOBALutils.identify_order(thar_co,order_dir+'fideos_20.iwdat',window=100,di=0.5) start_co = or20_co - 9 end_co = start_co + 38 - print '\t\tWill start on order', start_co, 'for comparison fibre...' - print '\t\tOffset for comparison fibre is ', offset_co, 'pixels' + print('\t\tWill start on order', start_co, 'for comparison fibre...') + print('\t\tOffset for comparison fibre is ', offset_co, 'pixels') thar_dict = {'start_ob':start_ob,'end_ob':end_ob,'start_co':start_co,\ 'end_co':end_co, 'offset_ob':offset_ob, 'offset_co':offset_co,'start_ob_MgIII':start_ob_MgIII,'end_ob_MgIII':end_ob_MgIII} pickle.dump( thar_dict, open( dirout+'order_id.pkl', 'w' ) ) else: - print '\t\tValues loaded from file...' + print('\t\tValues loaded from file...') thar_dict = pickle.load( open( dirout+'order_id.pkl', 'r' ) ) start_ob = thar_dict['start_ob'] end_ob = thar_dict['end_ob'] @@ -551,19 +552,19 @@ offset_co = thar_dict['offset_co'] start_ob_MgIII = thar_dict['start_ob_MgIII'] end_ob_MgIII = thar_dict['end_ob_MgIII'] - - print '\t\tWill start on order', start_ob, 'for object fibre...' - print '\t\tOffset for object fibre is ', offset_ob, 'pixels' - print '\t\tWill start on order', start_co, 'for comparison fibre...' - print '\t\tOffset for comparison fibre is ', offset_co, 'pixels' + + print('\t\tWill start on order', start_ob, 'for object fibre...') + print('\t\tOffset for object fibre is ', offset_ob, 'pixels') + print('\t\tWill start on order', start_co, 'for comparison fibre...') + print('\t\tOffset for comparison fibre is ', offset_co, 'pixels') ntot = end_ob - start_ob +1 ntot2 = end_co - start_co +1 -print 'ntot',ntot,ntot2 +print('ntot',ntot,ntot2) ntot_MgIII = end_ob_MgIII - start_ob_MgIII + 1 -print start_ob, end_ob -print start_ob_MgIII, end_ob_MgIII +print(start_ob, end_ob) +print(start_ob_MgIII, end_ob_MgIII) #force_thar_wavcal = True for fsim in thars: thar_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' @@ -572,7 +573,7 @@ thar_spec_co = dirout + fsim.split('/')[-1][:-4]+'sp.co.fits' wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "Working on initial ThAr file", fsim + print("Working on initial ThAr file", fsim) hthar = pyfits.open( fsim ) mjd, mjd0 = fideosutils.mjd_fromheader( hthar ) thar_fits_ob = dirout + fsim.split('/')[-1][:-4]+'spec.ob.fits.S' @@ -597,7 +598,7 @@ All_Centroids_co = np.array([]) All_Sigmas_co = np.array([]) All_Intensities_co = np.array([]) - + if not just_comp: #offset_ob = GLOBALutils.get_rough_offset(lines_thar_ob,reffiles,window=300) #offset_co = GLOBALutils.get_rough_offset(lines_thar_co,reffiles,window=300) @@ -611,7 +612,7 @@ thar_order_orig = lines_thar_ob[order,:]/scipy.signal.medfilt(S_flat_ob[0].data[order,1],31) IV = iv_thar_ob[order,:] wei = np.ones(len(thar_order_orig))#np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) + bkg = scipy.signal.medfilt(thar_order_orig,101) thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ @@ -621,7 +622,7 @@ #plot(pixel_centers,residuals,'.') #plot([0,2048],[0,0],'r') #show() - if (idorder == 25): + if (idorder == 25): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) @@ -639,7 +640,7 @@ #GLOBALutils.get_zero_order_number(t_ords,t_wavs) p0 = np.zeros( npar_wsol ) - p0[0] = (25+49) * Global_ZP + p0[0] = (25+49) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ @@ -655,17 +656,17 @@ #meanres.append(np.median(G_res[I])) #meanwav.append(np.median(G_wav[I])) #plot(meanres,'o') - axhline(0) - xticks(fontsize=25) - yticks(fontsize=25) - xlabel(r'Wavelength [$\AA$]', fontsize=25) - ylabel(r'Residuals [$\AA$]', fontsize=25) + axhline(0) + xticks(fontsize=25) + yticks(fontsize=25) + xlabel(r'Wavelength [$\AA$]', fontsize=25) + ylabel(r'Residuals [$\AA$]', fontsize=25) show() #delt +=0.001 """ - #print fds + #print fds thar_out = np.zeros((2,ntot,lines_thar_ob.shape[1])) - equis = np.arange( lines_thar_ob.shape[1] ) + equis = np.arange( lines_thar_ob.shape[1] ) order = start_ob idorder = 11 out_order = 0 @@ -675,20 +676,20 @@ WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) thar_out[0,out_order,:] = WavSol thar_out[1,out_order,:] = lines_thar_ob[order] - #plot(equis,WavSol,'k') - + #plot(equis,WavSol,'k') + order+=1 idorder += 1 out_order += 1 wsdate = str(datetime.datetime.now()).replace(' ','T') - #plot(All_Pixel_Centers,All_Wavelengths,'ro') - #xticks(fontsize=25) - #yticks(fontsize=25) - #xlabel('Pixel', fontsize=25) - #ylabel(r'Wavelength [$\AA$]', fontsize=25) - #show() - #print tfrds - + #plot(All_Pixel_Centers,All_Wavelengths,'ro') + #xticks(fontsize=25) + #yticks(fontsize=25) + #xlabel('Pixel', fontsize=25) + #ylabel(r'Wavelength [$\AA$]', fontsize=25) + #show() + #print tfrds + if os.access(thar_spec_ob,os.F_OK): os.system('rm '+ thar_spec_ob) hdu = pyfits.PrimaryHDU(thar_out) @@ -704,11 +705,11 @@ order_s = str(idorder) if (idorder < 10): order_s = '0'+str(idorder) - + thar_order_orig = lines_thar_co[order,:]/scipy.signal.medfilt(S_flat_co[0].data[order,1],31) IV = iv_thar_co[order,:] wei = np.ones(len(thar_order_orig))#np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) + bkg = scipy.signal.medfilt(thar_order_orig,101) thar_order = thar_order_orig - bkg IO = np.where(G_ord == idorder)[0] #print idorder @@ -727,7 +728,7 @@ coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ = fideosutils.Initial_Wav_Calibration( order_dir+'fideos_co_'+order_s+'.iwdat', thar_order, order, wei, \ rmsmax=400, minlines=10,FixEnds=False,Dump_Argon=False,Cheby=True,porder=ncoef_x,rough_shift = offset_co,do_xc=False,line_width=6,pixelization=True) - #if (idorder == 25): + #if (idorder == 25): # Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) @@ -740,7 +741,7 @@ idorder += 1 #p0 = np.zeros( npar_wsol ) - #p0[0] = (25+49) * Global_ZP + #p0[0] = (25+49) * Global_ZP p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ np.ones(All_Intensities_co.shape), p1, Cheby=True,\ @@ -754,7 +755,7 @@ #plot(meanres,'o') thar_out_co = np.zeros((2,nord_co,lines_thar_co.shape[1])) - equis = np.arange( lines_thar_co.shape[1] ) + equis = np.arange( lines_thar_co.shape[1] ) order = start_co idorder = 11 out_order = 0 @@ -774,7 +775,7 @@ hdu = GLOBALutils.update_header(hdu,'THDATE',thar_S_co[0].header['DATE']) hdu = GLOBALutils.update_header(hdu,'DATE',wsdate) hdu.writeto(thar_spec_co) - + if not just_comp: pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas,\ @@ -789,93 +790,93 @@ pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "Using previously computed wavelength solution in file",wavsol_pkl + print("Using previously computed wavelength solution in file",wavsol_pkl) thar_out = pyfits.getdata(thar_spec_ob) if fsim == thars[0]: ref_pix = thar_out[0,25,1024] if DoFull: - ## start Wavsol for Mg triplet - for fsim in thars: - thar_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' - wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' - dct = pickle.load(open(wavsol_pkl,'r')) - if not ( 'p1_MgIII' in dct.keys() ) or (force_thar_wavcal): - print "Wavelength solution including reddest orders of", fsim - hthar = pyfits.open( fsim ) - thar_fits_ob = dirout + fsim.split('/')[-1][:-4]+'spec.ob.fits.S' - thar_S_ob = pyfits.open( thar_fits_ob ) - - lines_thar_ob = thar_S_ob[0].data[:,1,:] - iv_thar_ob = thar_S_ob[0].data[:,2,:] - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - - if not just_comp: - t_ords,t_wavs = [],[] - order = start_ob_MgIII - idorder = 0 - while order <= end_ob_MgIII: - order_s = str(idorder) - if (idorder < 10): - order_s = '0'+str(idorder) - thar_order_orig = lines_thar_ob[order,:]/scipy.signal.medfilt(S_flat_ob[0].data[order,1],31) - IV = iv_thar_ob[order,:] - wei = np.ones(len(thar_order_orig))#np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'fideos_'+order_s+'.iwdat', thar_order, order, wei, \ - rmsmax=400, sigmai=2.2,minlines=10,FixEnds=False,Dump_Argon=False,\ - Cheby=True, porder=ncoef_x, rough_shift=offset_ob, line_width=6, \ - do_xc=False, pixelization=True) - - if (idorder == 25): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + idorder ) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - - t_ords.append(order) - t_wavs.append(GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) )) - order += 1 - idorder += 1 - t_ords,t_wavs = np.array(t_ords),np.array(t_wavs) - - #GLOBALutils.get_zero_order_number(t_ords,t_wavs) - p0 = np.zeros( npar_wsol ) - p0[0] = (25+49) * Global_ZP - - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ - np.ones(All_Intensities.shape), p0, Cheby=True,\ - maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_ob,order0=49, \ - ntotal=ntot_MgIII,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - dct['p1_MgIII'] = p1 - pickle.dump(dct, open( wavsol_pkl, 'w' )) - else: - print "Using previously computed wavelength solution of reddest orders for",wavsol_pkl - - ## end Wavsol for Mg triplet + ## start Wavsol for Mg triplet + for fsim in thars: + thar_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' + wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' + dct = pickle.load(open(wavsol_pkl,'r')) + if not ( 'p1_MgIII' in dct.keys() ) or (force_thar_wavcal): + print("Wavelength solution including reddest orders of", fsim) + hthar = pyfits.open( fsim ) + thar_fits_ob = dirout + fsim.split('/')[-1][:-4]+'spec.ob.fits.S' + thar_S_ob = pyfits.open( thar_fits_ob ) + + lines_thar_ob = thar_S_ob[0].data[:,1,:] + iv_thar_ob = thar_S_ob[0].data[:,2,:] + + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + + if not just_comp: + t_ords,t_wavs = [],[] + order = start_ob_MgIII + idorder = 0 + while order <= end_ob_MgIII: + order_s = str(idorder) + if (idorder < 10): + order_s = '0'+str(idorder) + thar_order_orig = lines_thar_ob[order,:]/scipy.signal.medfilt(S_flat_ob[0].data[order,1],31) + IV = iv_thar_ob[order,:] + wei = np.ones(len(thar_order_orig))#np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'fideos_'+order_s+'.iwdat', thar_order, order, wei, \ + rmsmax=400, sigmai=2.2,minlines=10,FixEnds=False,Dump_Argon=False,\ + Cheby=True, porder=ncoef_x, rough_shift=offset_ob, line_width=6, \ + do_xc=False, pixelization=True) + + if (idorder == 25): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + idorder ) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + + t_ords.append(order) + t_wavs.append(GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) )) + order += 1 + idorder += 1 + t_ords,t_wavs = np.array(t_ords),np.array(t_wavs) + + #GLOBALutils.get_zero_order_number(t_ords,t_wavs) + p0 = np.zeros( npar_wsol ) + p0[0] = (25+49) * Global_ZP + + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ + np.ones(All_Intensities.shape), p0, Cheby=True,\ + maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_ob,order0=49, \ + ntotal=ntot_MgIII,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + dct['p1_MgIII'] = p1 + pickle.dump(dct, open( wavsol_pkl, 'w' )) + else: + print("Using previously computed wavelength solution of reddest orders for",wavsol_pkl) + + ## end Wavsol for Mg triplet for fsim in thars_co: - print fsim + print(fsim) thar_fits_co_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.co.fits.S' thar_spec_co = dirout + fsim.split('/')[-1][:-4]+'sp.co.fits' wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "Working on initial ThAr file", fsim + print("Working on initial ThAr file", fsim) hthar = pyfits.open( fsim ) mjd, mjd0 = fideosutils.mjd_fromheader( hthar ) thar_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' @@ -890,7 +891,7 @@ All_Centroids_co = np.array([]) All_Sigmas_co = np.array([]) All_Intensities_co = np.array([]) - + #offset_ob = GLOBALutils.get_rough_offset(lines_thar_ob,reffiles,window=300) #offset_co = GLOBALutils.get_rough_offset(lines_thar_co,reffiles,window=300) @@ -900,17 +901,17 @@ order_s = str(idorder) if (idorder < 10): order_s = '0'+str(idorder) - + thar_order_orig = lines_thar_co[order,:]/scipy.signal.medfilt(S_flat_co[0].data[order,1],31) IV = iv_thar_co[order,:] wei = np.ones(len(thar_order_orig))#np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) + bkg = scipy.signal.medfilt(thar_order_orig,101) thar_order = thar_order_orig - bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ = fideosutils.Initial_Wav_Calibration( order_dir+'fideos_co_'+order_s+'.iwdat', thar_order, order, wei, \ rmsmax=400, minlines=10,FixEnds=False,Dump_Argon=False,Cheby=True,porder=ncoef_x,rough_shift = offset_co,do_xc=False,line_width=6,pixelization=True) - if (idorder == 25): + if (idorder == 25): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) @@ -923,7 +924,7 @@ idorder += 1 p0 = np.zeros( npar_wsol ) - p0[0] = (25+49) * Global_ZP + p0[0] = (25+49) * Global_ZP p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ np.ones(All_Intensities_co.shape), p0, Cheby=True,\ @@ -931,7 +932,7 @@ ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) thar_out_co = np.zeros((2,nord_co,lines_thar_co.shape[1])) - equis = np.arange( lines_thar_co.shape[1] ) + equis = np.arange( lines_thar_co.shape[1] ) order = start_co idorder = 11 out_order = 0 @@ -951,14 +952,14 @@ hdu = GLOBALutils.update_header(hdu,'THDATE',thar_S_co[0].header['DATE']) hdu = GLOBALutils.update_header(hdu,'DATE',wsdate) hdu.writeto(thar_spec_co) - + pdict = {'mjd':mjd, 'p1_co':p1_co, 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, \ 'G_wav_co':G_wav_co, 'II_co':II_co, 'rms_ms_co':rms_ms_co,'G_res_co':G_res_co, \ 'All_Orders_co':All_Orders_co, 'All_Pixel_Centers_co': All_Pixel_Centers_co, 'All_Wavelengths_co':All_Wavelengths_co } pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "Using previously computed wavelength solution in file",wavsol_pkl + print("Using previously computed wavelength solution in file",wavsol_pkl) drift_file = dirout + '/drifts.txt' drift_names_file = dirout + '/drifts_names.txt' @@ -969,11 +970,11 @@ force_drifts = False if os.path.isfile(drift_names_file): - orednames = np.loadtxt(drift_names_file,dtype='str')[:-1] - for thar in thars: - if not thar.split('/')[-1] in orednames: - force_drifts = True - break + orednames = np.loadtxt(drift_names_file,dtype='str')[:-1] + for thar in thars: + if not thar.split('/')[-1] in orednames: + force_drifts = True + break drift_out = [] drift_out_co = [] @@ -982,157 +983,157 @@ mindev = 10000000. best_id = 0 if len(thars)>0: - if force_drifts == True or os.path.isfile(drift_names_file) == False or os.path.isfile(drift_file) == False: - for refthid in range(len(thars)): - rednames.append(thars[refthid].split('/')[-1]) - #refthid = 100 - #refthid = int(.5*len(thars))+1 - #print 'Reference wavelength image will be:', thars[refthid] - pkl_wsol = dirout + thars[refthid].split('/')[-1][:-4]+'wavsolpars.pkl' - #print " Unpickling wavelength solution from", pkl_wsol, " ..." - wsol_dict = pickle.load(open(pkl_wsol,'r')) - sci_all = np.hstack((sim_sci, dar_sci)) - #sci_all = sci_one.copy() - thar_fits_co = dirout + thars[refthid].split('/')[-1][:-4]+'spec.co.fits.S' - thar_S_co = pyfits.getdata( thar_fits_co ) - - print '\nComputing instrumental drifts for ThAr + ThAr imeges...' - - for i in range(len(thars)): - texp = pyfits.getheader(thars[i])['EXPTIME'] - pkl = dirout + thars[i].split('/')[-1][:-4]+'wavsolpars.pkl' - wsol = pickle.load(open(pkl,'r')) - mjd, mjd0 = fideosutils.mjd_fromheader( pyfits.open(thars[i]) ) - p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(wsol['All_Pixel_Centers_co'], wsol['All_Wavelengths_co'],\ - wsol['All_Orders_co'], np.ones(len(wsol['All_Orders_co'])), wsol_dict['p1_co'], Cheby=True, \ - Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co.shape[2],nx=ncoef_x,nm=ncoef_m) - werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) - rv_drift_co = (1e-6*p_shift_co)*299792458.0 - mat_co = np.array([mjd, rv_drift_co, werr_co,texp,0]) - - if not just_comp: - p_shift_ob, pix_centers, orders, wavelengths, I, rms_ms_ob, residuals_ob = \ - GLOBALutils.Global_Wav_Solution_vel_shift(wsol['All_Pixel_Centers'], wsol['All_Wavelengths'],\ - wsol['All_Orders'], np.ones(len(wsol['All_Orders'])), wsol_dict['p1'], Cheby=True, \ - Inv=True,maxrms=MRMS,minlines=minlines_glob_ob, order0=49,ntotal=ntot,npix=thar_S_co.shape[2],nx=ncoef_x,nm=ncoef_m) - print '\n' - werr_ob = rms_ms_ob/np.sqrt(float(len(residuals_ob))) - rv_drift_ob = (1e-6*p_shift_ob)*299792458.0 - mat = np.array([mjd,rv_drift_ob, werr_ob, rv_drift_co, werr_co,texp]) - - else: - print '\n' - mat = np.array([thar_times[i], rv_drift_co, werr_co, texp]) - - if True:#werr_co < 5: - if len(drift_out_co)==0: - drift_out_co = mat_co.copy() - else: - drift_out_co = np.vstack((drift_out_co,mat_co)) - if True:#werr_ob < 5: - if len(drift_out)==0: - drift_out = mat.copy() - else: - drift_out = np.vstack((drift_out,mat)) - deviation = np.absolute(np.mean(drift_out[:,1] - drift_out[:,3])) - print deviation, mindev - if deviation < mindev: - mindev = deviation - drift_out_final = drift_out.copy() - best_id = refthid - rednames.append(str(refthid)+','+str(mindev)) - np.savetxt(drift_names_file,np.array(rednames),fmt="%s") - np.savetxt(drift_file,drift_out_final) - else: - savedref = np.loadtxt(drift_names_file,dtype='str')[-1] - best_id = int(savedref.split(',')[0]) - mindev = float(savedref.split(',')[1]) - -print 'Reference wavelength image will be:', thars[best_id] -print 'Mean diference between science and comparison fiber is', mindev, 'm/s' + if force_drifts == True or os.path.isfile(drift_names_file) == False or os.path.isfile(drift_file) == False: + for refthid in range(len(thars)): + rednames.append(thars[refthid].split('/')[-1]) + #refthid = 100 + #refthid = int(.5*len(thars))+1 + #print 'Reference wavelength image will be:', thars[refthid] + pkl_wsol = dirout + thars[refthid].split('/')[-1][:-4]+'wavsolpars.pkl' + #print " Unpickling wavelength solution from", pkl_wsol, " ..." + wsol_dict = pickle.load(open(pkl_wsol,'r')) + sci_all = np.hstack((sim_sci, dar_sci)) + #sci_all = sci_one.copy() + thar_fits_co = dirout + thars[refthid].split('/')[-1][:-4]+'spec.co.fits.S' + thar_S_co = pyfits.getdata( thar_fits_co ) + + print('\nComputing instrumental drifts for ThAr + ThAr imeges...') + + for i in range(len(thars)): + texp = pyfits.getheader(thars[i])['EXPTIME'] + pkl = dirout + thars[i].split('/')[-1][:-4]+'wavsolpars.pkl' + wsol = pickle.load(open(pkl,'r')) + mjd, mjd0 = fideosutils.mjd_fromheader( pyfits.open(thars[i]) ) + p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(wsol['All_Pixel_Centers_co'], wsol['All_Wavelengths_co'],\ + wsol['All_Orders_co'], np.ones(len(wsol['All_Orders_co'])), wsol_dict['p1_co'], Cheby=True, \ + Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co.shape[2],nx=ncoef_x,nm=ncoef_m) + werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) + rv_drift_co = (1e-6*p_shift_co)*299792458.0 + mat_co = np.array([mjd, rv_drift_co, werr_co,texp,0]) + + if not just_comp: + p_shift_ob, pix_centers, orders, wavelengths, I, rms_ms_ob, residuals_ob = \ + GLOBALutils.Global_Wav_Solution_vel_shift(wsol['All_Pixel_Centers'], wsol['All_Wavelengths'],\ + wsol['All_Orders'], np.ones(len(wsol['All_Orders'])), wsol_dict['p1'], Cheby=True, \ + Inv=True,maxrms=MRMS,minlines=minlines_glob_ob, order0=49,ntotal=ntot,npix=thar_S_co.shape[2],nx=ncoef_x,nm=ncoef_m) + print('\n') + werr_ob = rms_ms_ob/np.sqrt(float(len(residuals_ob))) + rv_drift_ob = (1e-6*p_shift_ob)*299792458.0 + mat = np.array([mjd,rv_drift_ob, werr_ob, rv_drift_co, werr_co,texp]) + + else: + print('\n') + mat = np.array([thar_times[i], rv_drift_co, werr_co, texp]) + + if True:#werr_co < 5: + if len(drift_out_co)==0: + drift_out_co = mat_co.copy() + else: + drift_out_co = np.vstack((drift_out_co,mat_co)) + if True:#werr_ob < 5: + if len(drift_out)==0: + drift_out = mat.copy() + else: + drift_out = np.vstack((drift_out,mat)) + deviation = np.absolute(np.mean(drift_out[:,1] - drift_out[:,3])) + print(deviation, mindev) + if deviation < mindev: + mindev = deviation + drift_out_final = drift_out.copy() + best_id = refthid + rednames.append(str(refthid)+','+str(mindev)) + np.savetxt(drift_names_file,np.array(rednames),fmt="%s") + np.savetxt(drift_file,drift_out_final) + else: + savedref = np.loadtxt(drift_names_file,dtype='str')[-1] + best_id = int(savedref.split(',')[0]) + mindev = float(savedref.split(',')[1]) + +print('Reference wavelength image will be:', thars[best_id]) +print('Mean diference between science and comparison fiber is', mindev, 'm/s') pkl_wsol = dirout + thars[best_id].split('/')[-1][:-4]+'wavsolpars.pkl' -print " Unpickling wavelength solution from", pkl_wsol, " ..." +print(" Unpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) #print thars_co -print '\nComputing instrumental drifts for ThAr + Dark imeges...' -print 'Reference wavelength image will be:', thars[best_id] +print('\nComputing instrumental drifts for ThAr + Dark imeges...') +print('Reference wavelength image will be:', thars[best_id]) pkl_wsol2 = dirout + thars[best_id].split('/')[-1][:-4]+'wavsolpars.pkl' -print " Unpickling wavelength solution from", pkl_wsol2, " ..." +print(" Unpickling wavelength solution from", pkl_wsol2, " ...") wsol_dict2 = pickle.load(open(pkl_wsol2,'r')) sci_all = np.hstack((sim_sci, dar_sci)) if len(thars_co)>0: - thar_fits_co2 = dirout + thars_co[int(.5*len(thars_co))].split('/')[-1][:-4]+'spec.co.fits.S' - thar_S_co2 = pyfits.getdata( thar_fits_co2 ) - - for i in range(len(thars_co)): - texp = pyfits.getheader(thars_co[i])['EXPTIME'] - pkl = dirout + thars_co[i].split('/')[-1][:-4]+'wavsolpars.pkl' - wsol = pickle.load(open(pkl,'r')) - hthar = pyfits.open( thars_co[i] ) - mjd, mjd0 = fideosutils.mjd_fromheader( hthar ) - p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(wsol['G_pix_co'], wsol['G_wav_co'],\ - wsol['G_ord_co'], np.ones(len(wsol['G_ord_co'])), wsol_dict2['p1_co'], Cheby=True, \ - Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) - werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) - rv_drift_co = (1e-6*p_shift_co)*299792458.0 - mat_co = np.array([mjd, rv_drift_co, werr_co,texp,0]) - - if werr_co<5: - if len(drift_out_co)==0: - drift_out_co = mat_co.copy() - else: - drift_out_co = np.vstack((drift_out_co,mat_co)) - np.savetxt(drift_file_co,drift_out_co) + thar_fits_co2 = dirout + thars_co[int(.5*len(thars_co))].split('/')[-1][:-4]+'spec.co.fits.S' + thar_S_co2 = pyfits.getdata( thar_fits_co2 ) + + for i in range(len(thars_co)): + texp = pyfits.getheader(thars_co[i])['EXPTIME'] + pkl = dirout + thars_co[i].split('/')[-1][:-4]+'wavsolpars.pkl' + wsol = pickle.load(open(pkl,'r')) + hthar = pyfits.open( thars_co[i] ) + mjd, mjd0 = fideosutils.mjd_fromheader( hthar ) + p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(wsol['G_pix_co'], wsol['G_wav_co'],\ + wsol['G_ord_co'], np.ones(len(wsol['G_ord_co'])), wsol_dict2['p1_co'], Cheby=True, \ + Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) + werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) + rv_drift_co = (1e-6*p_shift_co)*299792458.0 + mat_co = np.array([mjd, rv_drift_co, werr_co,texp,0]) + + if werr_co<5: + if len(drift_out_co)==0: + drift_out_co = mat_co.copy() + else: + drift_out_co = np.vstack((drift_out_co,mat_co)) + np.savetxt(drift_file_co,drift_out_co) if absref != 'none' and os.path.isfile(absref): - pkl0 = pickle.load(open(absref,'r')) - pkl = pickle.load(open(pkl_wsol2,'r')) - thar_fits_co2 = dirout + thars[int(.5*len(thars))].split('/')[-1][:-4]+'spec.co.fits.S' - thar_S_co2 = pyfits.getdata( thar_fits_co2 ) - p_shift, pix_centers, orders, wavelengths, I, rms_ms_ob, residuals_ob = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pkl['G_pix'], pkl['G_wav'],\ - pkl['G_ord'], np.ones(len(pkl['G_ord'])), pkl0['p1'], Cheby=True, \ - Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) - werr_ob = rms_ms_ob/np.sqrt(float(len(residuals_ob))) - p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pkl['G_pix_co'], pkl['G_wav_co'],\ - pkl['G_ord_co'], np.ones(len(pkl['G_ord_co'])), pkl0['p1_co'], Cheby=True, \ - Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) - werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) - drift_ob0 = (1e-6*p_shift)*299792458.0 - drift_co0 = (1e-6*p_shift_co)*299792458.0 - print drift_ob0,werr_ob - print drift_co0,werr_co - thar_dict = pickle.load( open( dirout+'order_id.pkl', 'r' ) ) - thar_dict['absref'] = absref - thar_dict['absp_shift_ob'] = drift_ob0 - thar_dict['absp_shift_co'] = drift_co0 - thar_dict['werr_ob'] = werr_ob - thar_dict['werr_co'] = werr_co - pickle.dump( thar_dict, open( dirout+'order_id.pkl', 'w' ) ) -print 'HAAAA' + pkl0 = pickle.load(open(absref,'r')) + pkl = pickle.load(open(pkl_wsol2,'r')) + thar_fits_co2 = dirout + thars[int(.5*len(thars))].split('/')[-1][:-4]+'spec.co.fits.S' + thar_S_co2 = pyfits.getdata( thar_fits_co2 ) + p_shift, pix_centers, orders, wavelengths, I, rms_ms_ob, residuals_ob = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pkl['G_pix'], pkl['G_wav'],\ + pkl['G_ord'], np.ones(len(pkl['G_ord'])), pkl0['p1'], Cheby=True, \ + Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) + werr_ob = rms_ms_ob/np.sqrt(float(len(residuals_ob))) + p_shift_co, pix_centers, orders, wavelengths, I, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pkl['G_pix_co'], pkl['G_wav_co'],\ + pkl['G_ord_co'], np.ones(len(pkl['G_ord_co'])), pkl0['p1_co'], Cheby=True, \ + Inv=True,maxrms=MRMS,minlines=minlines_glob_co, order0=49,ntotal=ntot,npix=thar_S_co2.shape[2],nx=ncoef_x,nm=ncoef_m) + werr_co = rms_ms_co/np.sqrt(float(len(residuals_co))) + drift_ob0 = (1e-6*p_shift)*299792458.0 + drift_co0 = (1e-6*p_shift_co)*299792458.0 + print(drift_ob0,werr_ob) + print(drift_co0,werr_co) + thar_dict = pickle.load( open( dirout+'order_id.pkl', 'r' ) ) + thar_dict['absref'] = absref + thar_dict['absp_shift_ob'] = drift_ob0 + thar_dict['absp_shift_co'] = drift_co0 + thar_dict['werr_ob'] = werr_ob + thar_dict['werr_co'] = werr_co + pickle.dump( thar_dict, open( dirout+'order_id.pkl', 'w' ) ) +print('HAAAA') if just_thar: - sys.exit() + sys.exit() ### start of science frame reductions ### justdo = np.array([]) if os.path.isfile(dirin+'/justdo.txt'): - justdo = open(dirin+'/justdo.txt').readlines() -print justdo + justdo = open(dirin+'/justdo.txt').readlines() +print(justdo) if len(justdo) != 0: - njustdo = [] - for jso in justdo: - njsm = dirin+'/'+jso[:-1] - njsm = njsm.replace('//','/') - njustdo.append(njsm) - njustdo = np.array(njustdo) - sci_all = njustdo.copy() -print sci_all + njustdo = [] + for jso in justdo: + njsm = dirin+'/'+jso[:-1] + njsm = njsm.replace('//','/') + njustdo.append(njsm) + njustdo = np.array(njustdo) + sci_all = njustdo.copy() +print(sci_all) new_list = [] new_list_obnames = [] new_list_texp = [] @@ -1155,9 +1156,9 @@ new_list_obnames.append( obname ) new_list_texp.append( texp ) -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') for nlisti in range(len(new_list)): - print '\t\t'+new_list_obnames[nlisti]+'\t'+new_list[nlisti] + print('\t\t'+new_list_obnames[nlisti]+'\t'+new_list[nlisti]) # Does any image have a special requirement for dealing with the moonlight? @@ -1180,763 +1181,763 @@ use_moon = np.array(use_moon) for fsim in new_list: try: - print '\n' - print "\t--> Working on image: ", fsim - hd = pyfits.getheader(fsim) - - hsim = pyfits.open(fsim) - mjd, mjd0 = fideosutils.mjd_fromheader( hsim ) - - exptime = hd['EXPTIME'] - #obname = fsim.split('/')[-1] - #obname = obname.split('-')[-1][:-5] - obname = fideosutils.get_name(fsim) - - print "\t\tObject name:",obname - - know_moon = False - if fsim.split('/')[-1] in spec_moon: - I = np.where(fsim.split('/')[-1] == spec_moon)[0] - know_moon = True - here_moon = use_moon[I] - - altitude = 2335. - latitude = -29.2543 - longitude = -70.7346 - epoch = 2000.0 - - known_coords = False - try: - sp,ra,dec,known_coords = GLOBALutils.simbad_coords(obname,mjd) - except: - ra,dec = -999,-999 - ras,decs = ra,dec - ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) - if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 - else: - print '\t\tUsing the coordinates found in the image header.' - - airmass = fideosutils.get_airmass(ra,dec,latitude,longitude,altitude,hd['DATE-OBS'].replace('T',' ')) - - iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) - obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) - obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) - - jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) - jplephem.set_observer_coordinates( float(obpos[0]), float(obpos[1]), float(obpos[2]) ) - - res = jplephem.doppler_fraction(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) - lbary_ltopo = 1.0 + res['frac'][0] - bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - - print "\t\tBarycentric velocity:", bcvel_baryc - - res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) - mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) - - # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name = 'ESO1.0' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) - - #date = hd['DATE-OBS'] - #date = datetime.datetime(int(date[:4]),int(date[5:7]),int(date[8:10]),int(date[11:13]),int(date[14:16]),int(date[17:19])) - #new_date = date - #OJO aquiiiiii - #print 'Warning!!! adding 5 hrs to comute MJD due to problem in header! CHECK in future!!' - #new_date = date + datetime.timedelta(hours=5) - - #gobs.date = new_date.strftime('%Y-%m-%d %H:%M:%S') - gobs.date = hd['DATE-OBS'][:10] + ' ' + hd['DATE-OBS'][11:] - mephem = ephem.Moon() - mephem.compute(gobs) - Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) - res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) - lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) - refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel - - if os.access(dirout+'Dark_'+str(exptime)+'.fits',os.F_OK): - DARK = pyfits.getdata(dirout+'Dark_'+str(exptime)+'.fits') - else: - DARK = np.zeros(MasterBias[0].data.shape) - - hdat = pyfits.open( fsim ) - data = pyfits.getdata( fsim ) - if data.shape == (2048, 2064): - data = data.T - data = data - MasterBias[0].data #- DARK - - data = np.fliplr(data.T) - #imshow(data.T,vmin=np.median(data),vmax=np.median(data)+500) - #for i in range(nord_ob): - # ejx = np.arange(2048) - # y = np.polyval(c_ob[i],ejx) - # plot(ejx,y) - #show() - - """ - drift, c_ob_new = GLOBALutils.get_drift(data.T,P_ob,c_ob,pii=1000,win=5) - - #drift = 2.4 - print '\t\tEstimated y drift:', drift - drift = 0. - c_ob_new = c_ob.copy() - c_ob_new[:,-1] += drift - c_co_new = c_co.copy() - c_co_new[:,-1] += drift - """ - c_ob_new = c_ob.copy() - c_co_new = c_co.copy() - P_ob_new = P_ob.copy()#GLOBALutils.shift_P(P_ob,drift,c_ob_new,ext_aperture_ob) - P_co_new = P_co.copy()#GLOBALutils.shift_P(P_co,drift,c_co_new,ext_aperture_co) - - #plot(P_ob_new[:,1000]) - #plot(P_ob[:,1000]) - #show() - bac_fits = dirout + 'BKG_'+ fsim.split('/')[-1][:-4]+'.fits' - - if os.access(bac_fits,os.F_OK) == False: - if fsim in sim_sci: - bac = fideosutils.get_scat(data, P_ob_new + P_co_new,1) - else: - bac = fideosutils.get_scat(data, P_ob_new,1) - hdu = pyfits.PrimaryHDU(bac) - hdu.writeto(bac_fits) - else: - bac = pyfits.getdata(bac_fits) - - #imshow(bac) - #show() - #plot(data[1000]) - #plot(bac[1000]) - #show() - data -= bac - #plot(data[1000]) - #show() - sci_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' - sci_fits_co_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.co.fits.S' - bac_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.bac.ob.fits.S' - sci_fits_ob = dirout + fsim.split('/')[-1][:-4]+'spec.ob.fits.S' - sci_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' - thar_spec_co = dirout + fsim.split('/')[-1][:-4]+'sp.co.fits' - - if ( os.access(sci_fits_ob,os.F_OK) == False ) or ( os.access(sci_fits_co,os.F_OK) == False ) or \ - ( os.access(sci_fits_ob_simple,os.F_OK) == False ) or ( os.access(sci_fits_co_simple,os.F_OK) == False ) or \ - ( force_sci_extract ): - print "No previous extraction or extraction forced for science file", fsim, "extracting..." - - sci_Ss_ob = GLOBALutils.simple_extraction(data.T,c_ob_new,ext_aperture_ob,min_extract_col,max_extract_col,npools) - bac_ob = GLOBALutils.simple_extraction(bac.T,c_ob_new,ext_aperture_ob,min_extract_col,max_extract_col,npools) - - sci_S_ob = GLOBALutils.optimal_extraction(data.T,P_ob_new,c_ob_new,ext_aperture_ob,ronoise,gain,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) - sci_Ss_ob = GLOBALutils.invert(sci_Ss_ob) - sci_S_ob = GLOBALutils.invert(sci_S_ob) - if (os.access(sci_fits_ob_simple,os.F_OK)): - os.remove( sci_fits_ob_simple ) - if (os.access(sci_fits_ob,os.F_OK)): - os.remove( sci_fits_ob ) - if (os.access(bac_fits_ob_simple,os.F_OK)): - os.remove( bac_fits_ob_simple ) - - hdu = pyfits.PrimaryHDU( sci_S_ob ) - hdu.writeto( sci_fits_ob ) - hdu = pyfits.PrimaryHDU( sci_Ss_ob ) - hdu.writeto( sci_fits_ob_simple ) - hdu = pyfits.PrimaryHDU( bac_ob ) - hdu.writeto( bac_fits_ob_simple ) - - if fsim in sim_sci: - sci_Ss_co = GLOBALutils.simple_extraction(data.T,c_co_new,ext_aperture_co,min_extract_col,max_extract_col,npools) - sci_S_co = GLOBALutils.optimal_extraction(data.T,P_co_new,c_co_new,ext_aperture_co,ronoise,gain,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) - sci_Ss_co = GLOBALutils.invert(sci_Ss_co) - sci_S_co = GLOBALutils.invert(sci_S_co) - if (os.access(sci_fits_co,os.F_OK)): - os.remove( sci_fits_co ) - if (os.access(sci_fits_co_simple,os.F_OK)): - os.remove( sci_fits_co_simple ) - hdu = pyfits.PrimaryHDU( sci_S_co ) - hdu.writeto( sci_fits_co ) - hdu = pyfits.PrimaryHDU( sci_Ss_co ) - hdu.writeto( sci_fits_co_simple ) - - else: - print fsim, "has already been extracted, reading in product fits files..." - sci_S_ob = pyfits.getdata( sci_fits_ob ) - sci_Ss_ob = pyfits.getdata( sci_fits_ob_simple ) - bac_ob = pyfits.getdata( bac_fits_ob_simple ) - - p_shift = 0. - - if fsim in sim_sci: - sci_S_co = pyfits.getdata( sci_fits_co ) - sci_Ss_co = pyfits.getdata( sci_fits_co_simple ) - - lines_thar_co = sci_S_co[:,1,:] - iv_thar_co = sci_S_co[:,2,:] - #offset_co = GLOBALutils.get_rough_offset(lines_thar_co,reffiles) - All_Pixel_Centers_co = np.array([]) - All_Wavelengths_co = np.array([]) - All_Orders_co = np.array([]) - All_Centroids_co = np.array([]) - All_Sigmas_co = np.array([]) - All_Intensities_co = np.array([]) - All_residuals_co = np.array([]) - - order = start_co - idorder = 11 - while order <= end_co: - order_s = str(idorder) - if (idorder < 10): - order_s = '0'+str(idorder) - thar_order_orig = lines_thar_co[order,:]/scipy.signal.medfilt(S_flat_co[0].data[order,1],31) - IV = iv_thar_co[order,:] - wei = np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) #FEROSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = fideosutils.Initial_Wav_Calibration( order_dir+'fideos_co_'+order_s+'.iwdat', thar_order, order, wei, \ - rmsmax=400, minlines=10,FixEnds=False,Dump_Argon=False,Cheby=True,porder=ncoef_x,rough_shift = offset_co,do_xc=False,line_width=6,pixelization=True) - #psh, pix_centers, wavelengthss, rms_mss, residualss = FEROSutils.Wav_Solution_vel_shift(wsol_dict['c_p2w_c'][order-o0], \ - # pixel_centers, wavelengths, maxrms=100, minlines=30, Cheby=use_cheby) - #shifts.append((1e-6*psh[0])*299792458.0) - if (idorder == 25): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) - - All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) - All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) - All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + idorder ) - All_Centroids_co = np.append( All_Centroids_co, centroids) - All_Sigmas_co = np.append( All_Sigmas_co, sigmas) - All_Intensities_co = np.append( All_Intensities_co, intensities ) - All_residuals_co = np.append( All_residuals_co, residuals ) - order+=1 - idorder += 1 - - p0 = np.zeros( npar_wsol ) - p0[0] = (25+49) * Global_ZP - - p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ - np.ones(All_Intensities_co.shape), p0, Cheby=True,\ - maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_co,order0=49, \ - ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - #shifts = np.array(shifts) - #shifts = FEROSutils.sigma_clip(shifts) - #the_sh = np.around(shifts.mean(),1) - #error_sh = np.around(np.sqrt(np.var(shifts)/float(len(shifts)-1)),1) - #print 'Shifts (per order):', the_sh, '+-', error_sh - thar_out_co = np.zeros((2,nord_co,lines_thar_co.shape[1])) - equis = np.arange( lines_thar_co.shape[1] ) - order = start_co - idorder = 11 - out_order = 0 - while order <= end_co: - m = idorder + 49 - chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1_co,chebs,ncoef_x,ncoef_m) - thar_out_co[0,out_order,:] = WavSol - thar_out_co[1,out_order,:] = lines_thar_co[order] - order+=1 - idorder += 1 - out_order += 1 - - if os.access(thar_spec_co,os.F_OK): - os.system('rm '+ thar_spec_co) - hdu = pyfits.PrimaryHDU(thar_out_co) - hdu.writeto(thar_spec_co) - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ - np.ones(All_Intensities_co.shape), wsol_dict['p1_co'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob_co,\ - order0=49,ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - werr_co = rms_ms/np.sqrt(float(len(residuals))) - rv_drift_co = (1e-6*p_shift)*299792458.0 - mat_co = np.array([mjd, rv_drift_co, werr_co,exptime,1]) - - shift_pix = ref_pix*(1.+rv_drift_co/299792458.0) - - if werr_co<6: - if len(drift_out_co)==0: - drift_out_co = mat_co.copy() - else: - drift_out_co = np.vstack((drift_out_co,mat_co)) - else: - p_shift = 0. - print '\t Warning!! RMS of wavelength solution is to high, setting Instrumental drift to 0 m/s...' - - np.savetxt(drift_file_co,drift_out_co) - - fout = 'proc/'+ fsim.split('/')[-1][:-4]+'sp.fits' - spec = np.zeros((11, ntot, data.shape[0])) - hdu = pyfits.PrimaryHDU( spec ) - - hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', hd['DATE-OBS'][:10] ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', hd['DATE-OBS'][11:]) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hd['EXPTIME']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',ras) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',decs) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA DEG',ras) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC DEG',decs) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) - hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DRIFT_CO',np.around(rv_drift_co[0],1)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DRIFT_CO_E',np.around(werr_co,1)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH AIRMASS',np.around(airmass.value,3)) - hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH REFWAV',thars[best_id].split('/')[-1]) - - equis = np.arange( data.shape[0] ) - out_order = 0 - order = start_ob - idorder = 11 - - while order <= end_ob: - m = idorder + 49 - chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot,npix=len(equis),nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) - spec[0,out_order,:] = GLOBALutils.ToVacuum(WavSol) - spec[1,out_order,:] = sci_S_ob[order,1, :] - #spec[1,order,:] = sci_Ss_ob[order, :] - spec[2,out_order,:] = sci_S_ob[order,2, :] - # Flat-fielded spectrum - fn = S_flat_ob_n[order,1,:] - L = np.where( fn > 0 ) - spec[3,out_order,:][L] = sci_S_ob[order,1,:][L] / S_flat_ob_n[order,1,:][L] - spec[4,out_order,:][L] = sci_S_ob[order,2,:][L] * ( S_flat_ob_n[order,1,:][L] ** 2 ) - ccoef = GLOBALutils.get_cont_single(spec[0,out_order],spec[3,out_order],spec[4,out_order],ll=1.5,lu=5,nc=3) - L = np.where( spec[1,out_order] != 0 ) - spec[5,out_order,:][L] = spec[3,out_order][L] / np.polyval(ccoef,spec[0,out_order][L]) - - nJ = np.where(np.isnan(spec[5,out_order])==True)[0] - nJ2 = np.where(np.isinf(spec[5,out_order])==True)[0] - spec[5,out_order,nJ] = 1.0 - spec[5,out_order,nJ2] = 1.0 - ratio = spec[3,out_order,:][L] / spec[5,out_order,:][L] - spec[6,out_order,:][L] = spec[4,out_order,:][L] * (ratio ** 2 ) - spec[7,out_order,:][L] = ratio - spec[8,out_order,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + bac_ob[order,:][L] / gain + 2*ext_aperture_ob*(ronoise/gain)**2 ) - II = np.where(spec[8,out_order]<0)[0] - if len(II)>0: - spec[8,out_order,II] = 0. - - rI = np.where(spec[5,out_order] > 1. + 8./spec[8,out_order]) - spec[5,out_order,rI] = 1. - rI1 = np.where(spec[5,out_order] > 10) - rI2 = np.where(spec[5,out_order] < -10) - spec[5,out_order,rI1] = 1. - spec[5,out_order,rI2] = 1. - spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) - dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - - spec[9,out_order,:][L] = spec[5,out_order,:][L] * (dlambda_dx[L] ** 1) - spec[10,out_order,:][L] = spec[6,out_order,:][L] / (dlambda_dx[L] ** 2) - - order += 1 - out_order += 1 - idorder += 1 - hdu = GLOBALutils.update_header(hdu,'SFDATE',MasterBias[0].header['DATE']) - if (os.access( dirout + fout,os.F_OK)): - os.remove( dirout + fout) - hdu.writeto( dirout + fout ) - - if DoFull: - fout_full = 'proc/'+ fsim.split('/')[-1][:-4]+'sp.full.fits' - spec_full = np.zeros((11, ntot_MgIII, data.shape[0])) - hdufull = pyfits.PrimaryHDU( spec_full ) - - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH MJD', mjd) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH MBJD', mbjd) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH SHUTTER START DATE', hd['DATE-OBS'][:10] ) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH SHUTTER START UT', hd['DATE-OBS'][11:]) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH TEXP (S)',hd['EXPTIME']) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH TARGET NAME', obname) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA',ras) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC',decs) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA DEG',ras) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC DEG',decs) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA BARY',ra) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC BARY',dec) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH EQUINOX',2000) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS LATITUDE',latitude) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS LONGITUDE',longitude) - hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS ALTITUDE',altitude) - #hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',airmass) - - equis = np.arange( data.shape[0] ) - out_order = 0 - order = start_ob_MgIII - idorder = 0 - - while order <= end_ob_MgIII: - #print order - m = idorder + 49 - chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot_MgIII,npix=len(equis),nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_MgIII'],chebs,ncoef_x,ncoef_m) - spec_full[0,out_order,:] = GLOBALutils.ToVacuum(WavSol) - spec_full[1,out_order,:] = sci_S_ob[order,1, :] - #spec[1,order,:] = sci_Ss_ob[order, :] - spec_full[2,out_order,:] = sci_S_ob[order,2, :] - # Flat-fielded spectrum - fn = S_flat_ob_n[order,1,:] - L = np.where( fn > 0 ) - spec_full[3,out_order,:][L] = sci_S_ob[order,1,:][L] / S_flat_ob_n[order,1,:][L] - spec_full[4,out_order,:][L] = sci_S_ob[order,2,:][L] * ( S_flat_ob_n[order,1,:][L] ** 2 ) - ccoef = GLOBALutils.get_cont_single(spec_full[0,out_order],spec_full[3,out_order],spec_full[4,out_order],ll=1.5,lu=5,nc=3) - L = np.where( spec_full[1,out_order] != 0 ) - spec_full[5,out_order,:][L] = spec_full[3,out_order][L] / np.polyval(ccoef,spec_full[0,out_order][L]) - - nJ = np.where(np.isnan(spec_full[5,out_order])==True)[0] - nJ2 = np.where(np.isinf(spec_full[5,out_order])==True)[0] - spec_full[5,out_order,nJ] = 1.0 - spec_full[5,out_order,nJ2] = 1.0 - ratio = spec_full[3,out_order,:][L] / spec_full[5,out_order,:][L] - spec_full[6,out_order,:][L] = spec_full[4,out_order,:][L] * (ratio ** 2 ) - spec_full[7,out_order,:][L] = ratio - spec_full[8,out_order,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + bac_ob[order,:][L] / gain + 2*ext_aperture_ob*(ronoise/gain)**2 ) - II = np.where(spec_full[8,out_order]<0)[0] - if len(II)>0: - spec_full[8,out_order,II] = 0. - - rI = np.where(spec_full[5,out_order] > 1. + 8./spec_full[8,out_order]) - spec_full[5,out_order,rI] = 1. - rI1 = np.where(spec_full[5,out_order] > 10) - rI2 = np.where(spec_full[5,out_order] < -10) - spec_full[5,out_order,rI1] = 1. - spec_full[5,out_order,rI2] = 1. - spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) - dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - - spec_full[9,out_order,:][L] = spec_full[5,out_order,:][L] * (dlambda_dx[L] ** 1) - spec_full[10,out_order,:][L] = spec_full[6,out_order,:][L] / (dlambda_dx[L] ** 2) - - order += 1 - out_order += 1 - idorder += 1 - hdufull = GLOBALutils.update_header(hdufull,'SFDATE',S_flat_ob[0].header['DATE']) - if (os.access( dirout + fout_full,os.F_OK)): - os.remove( dirout + fout_full) - hdufull.writeto( dirout + fout_full ) - - - if (not JustExtract): - - if DoClass: - print '\t\tSpectral Analysis:' - # spectral analysis - # First, query SIMBAD with the object name - query_success = False - sp_type_query = 'None' - #query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) - # Now, query SIMBAD by coordinates if above not successful - #if (not query_success): - # query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - #print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query - - hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) - pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' - - if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" - spec2 = spec.copy() - T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec2,model_path=models_path,npools=npools) - line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) - f = open(pars_file,'w') - f.write(line) - f.close() - else: - print "\t\t\tAtmospheric parameters loaded from file:" - T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 - - else: - T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 - - T_eff_epoch = T_eff - logg_epoch = logg - Z_epoch = Z - vsini_epoch = vsini - vel0_epoch = vel0 - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - - print "\t\tRadial Velocity analysis:" - # assign mask - sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - - # Read in mask - ml, mh, weight = np.loadtxt(mask,unpack=True) - ml_v = GLOBALutils.ToVacuum( ml ) - mh_v = GLOBALutils.ToVacuum( mh ) - - # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS - av_m = 0.5*( ml_v + mh_v ) - ml_v -= 1.5*(av_m - ml_v) - mh_v += 1.5*(mh_v - av_m) - mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m - - #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' - - disp = GLOBALutils.get_disp(obname, reffile=reffile) - if disp == 0: - known_sigma = False - if vsini != -999 and vsini > 4.: - disp = vsini - else: - disp = 4. - else: - known_sigma = True - - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' - cond = True - while (cond): - # first rough correlation to find the minimum - vels, xc_full, sn, nlines_ccf, W_ccf = \ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=200,vel_step=3,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=200) - v1,x1 = vels.copy(), xc_full.copy() - - for i in range(spec.shape[1]): - I = np.where((spec[0,i]>7030) & (spec[0,i] < 7050))[0] - if len(I)>0: - W_ccf[i] = 0 - I = np.where((spec[0,i]>6920) & (spec[0,i] < 6940))[0] - if len(I)>0: - W_ccf[i] = 0 - I = np.where((spec[0,i]>4300) & (spec[0,i] < 4340))[0] - if len(I)>0: - W_ccf[i] = 0 - - #I = np.where(W_ccf!=0)[0] - #W_ccf[I] = 1. - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf, start_order=2) - # Normalize the continuum of the CCF robustly with R - yy = scipy.signal.medfilt(xc_av,11) - pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) - tck1 = scipy.interpolate.splrep(vels,pred,k=1) - xc_av_orig = xc_av.copy() - xc_av /= pred - - vel0_xc = vels[ np.argmin( xc_av ) ] - rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ - xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc - - xc_av_rough = xc_av - vels_rough = vels - if disp > 30: - disp = 30. - vel_width = np.maximum( 20.0, 6*disp ) - - vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.1,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) - for i in range(spec.shape[1]): - I = np.where((spec[0,i]>7030) & (spec[0,i] < 7050))[0] - if len(I)>0: - W_ccf[i] = 0 - I = np.where((spec[0,i]>6920) & (spec[0,i] < 6940))[0] - if len(I)>0: - W_ccf[i] = 0 - I = np.where((spec[0,i]>4300) & (spec[0,i] < 4340))[0] - if len(I)>0: - W_ccf[i] = 0 - - #I = np.where(W_ccf!=0)[0] - #W_ccf[I] = 1. - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf, start_order=2) - pred = scipy.interpolate.splev(vels,tck1) - xc_av /= pred - #for i in range(xc_full.shape[1]): - # plot(v1,x1[:,i]) - # plot(vels,xc_full[:,i]) - # show() - - - if sp_type == 'M5': - moon_sig = 2.5 - elif sp_type == 'K5': - moon_sig = 3.3 - else: - moon_sig = 4.5 - - p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ - sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) - - #ldc = CoralieUtils.get_ldc(T_eff, logg, Z, 1.0, ldfile = 'lin_coe_sloan2.dat') - #p1R, ROTmodel = CoralieUtils.XC_Final_Fit_Rot( vels, xc_av, ldc = ldc, vsini = vsini ) - moonmatters = False - if (know_moon and here_moon): - moonmatters = True - ismoon = True - confused = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) - moon_flag = 1 - else: - confused = False - ismoon = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 - moon_flag = 0 - - bspan = GLOBALutils.calc_bss(vels,xc_av) - SP = bspan[0] - - #print 'Bisector span:', SP - if (not known_sigma): - disp = np.floor(p1gau[2]) - if (disp < 4.0): - disp = 4.0 - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - known_sigma = True - else: - cond = False - - xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} - - moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':hd['EXPTIME']} - - pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' - pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - - ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' - - if not avoid_plot: - GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - - SNR_5130 = np.median(spec[8,21,1000:1101] ) - if SNR_5130 < 1: - SNR_5130 = 1. - seeing = -999 - - if sp_type == 'G2': - if T_eff < 6000: - A = 0.06544 - B = 0.00146 - D = 0.24416 - C = 0.00181 - else: - A = 0.09821 - B = 0.00014 - D = 0.33491 - C = 0.00113 - elif sp_type == 'K5': - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 - else: - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 - - RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) - depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) - - RVerr2_ori = -999.0 - if depth_fact >= 1.: - RVerr2 = -999.000 - else: - if sp_type == 'G2': - depth_fact = (1 - 0.62) / (1 - depth_fact) - else: - depth_fact = (1 - 0.59) / (1 - depth_fact) - RVerr2 = RVerr * depth_fact - RVerr2 = np.sqrt(RVerr2**2+0.004**2) - #if (RVerr2 <= 0.004): - # RVerr2 = 0.004 - - #if not good_quality: - # RVerr2 = np.sqrt(0.03**2 + RVerr2**2) - - BSerr = D / float(np.round(SNR_5130)) + C - - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) - RVerr2 = np.around(RVerr2,4) - BSerr = np.around(BSerr,4) - - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) - - bjd_out = 2400000.5 + mbjd - T_eff_err = 100 - logg_err = 0.5 - Z_err = 0.5 - vsini_err = 2 - XC_min = np.abs(np.around(np.min(XCmodel),2)) - - SNR_5130 = np.around(SNR_5130) - SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) - - disp_epoch = np.around(p1gau_m[2],1) - hdu = GLOBALutils.update_header(hdu,'RV', RV) - hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) - hdu = GLOBALutils.update_header(hdu,'BS', BS) - hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) - hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) - hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) - hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) - hdu = GLOBALutils.update_header(hdu,'INST', 'FIDEOS') - hdu = GLOBALutils.update_header(hdu,'RESOL', '40000') - hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') - hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) - hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - hdu = GLOBALutils.update_header(hdu,'DATE', str(datetime.datetime.now()).replace(' ','T')) - - line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f fideos ceres 40000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %7.4f %7.4f %7.4f %s\n"%\ - (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - hd['EXPTIME'], SNR_5130_R, airmass, shift_pix, rv_drift_co, ccf_pdf) - f_res.write(line_out) - if (os.access( dirout + fout,os.F_OK)): - os.remove( dirout + fout) - hdu.writeto( dirout + fout ) - - #else: - # print "\t\tReading spectral file from", fout - # spec = pyfits.getdata( fout ) + print('\n') + print("\t--> Working on image: ", fsim) + hd = pyfits.getheader(fsim) + + hsim = pyfits.open(fsim) + mjd, mjd0 = fideosutils.mjd_fromheader( hsim ) + + exptime = hd['EXPTIME'] + #obname = fsim.split('/')[-1] + #obname = obname.split('-')[-1][:-5] + obname = fideosutils.get_name(fsim) + + print("\t\tObject name:",obname) + + know_moon = False + if fsim.split('/')[-1] in spec_moon: + I = np.where(fsim.split('/')[-1] == spec_moon)[0] + know_moon = True + here_moon = use_moon[I] + + altitude = 2335. + latitude = -29.2543 + longitude = -70.7346 + epoch = 2000.0 + + known_coords = False + try: + sp,ra,dec,known_coords = GLOBALutils.simbad_coords(obname,mjd) + except: + ra,dec = -999,-999 + ras,decs = ra,dec + ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) + if ra2 !=0 and dec2 != 0: + ra = ra2 + dec = dec2 + else: + print('\t\tUsing the coordinates found in the image header.') + + airmass = fideosutils.get_airmass(ra,dec,latitude,longitude,altitude,hd['DATE-OBS'].replace('T',' ')) + + iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) + obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) + obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) + + jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) + jplephem.set_observer_coordinates( float(obpos[0]), float(obpos[1]), float(obpos[2]) ) + + res = jplephem.doppler_fraction(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + lbary_ltopo = 1.0 + res['frac'][0] + bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 + + print("\t\tBarycentric velocity:", bcvel_baryc) + + res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) + + # Moon Phase Calculations + gobs = ephem.Observer() + gobs.name = 'ESO1.0' + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) + + #date = hd['DATE-OBS'] + #date = datetime.datetime(int(date[:4]),int(date[5:7]),int(date[8:10]),int(date[11:13]),int(date[14:16]),int(date[17:19])) + #new_date = date + #OJO aquiiiiii + #print 'Warning!!! adding 5 hrs to comute MJD due to problem in header! CHECK in future!!' + #new_date = date + datetime.timedelta(hours=5) + + #gobs.date = new_date.strftime('%Y-%m-%d %H:%M:%S') + gobs.date = hd['DATE-OBS'][:10] + ' ' + hd['DATE-OBS'][11:] + mephem = ephem.Moon() + mephem.compute(gobs) + Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) + res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) + lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) + refvel = bcvel_baryc + moonvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) + + if os.access(dirout+'Dark_'+str(exptime)+'.fits',os.F_OK): + DARK = pyfits.getdata(dirout+'Dark_'+str(exptime)+'.fits') + else: + DARK = np.zeros(MasterBias[0].data.shape) + + hdat = pyfits.open( fsim ) + data = pyfits.getdata( fsim ) + if data.shape == (2048, 2064): + data = data.T + data = data - MasterBias[0].data #- DARK + + data = np.fliplr(data.T) + #imshow(data.T,vmin=np.median(data),vmax=np.median(data)+500) + #for i in range(nord_ob): + # ejx = np.arange(2048) + # y = np.polyval(c_ob[i],ejx) + # plot(ejx,y) + #show() + + """ + drift, c_ob_new = GLOBALutils.get_drift(data.T,P_ob,c_ob,pii=1000,win=5) + + #drift = 2.4 + print '\t\tEstimated y drift:', drift + drift = 0. + c_ob_new = c_ob.copy() + c_ob_new[:,-1] += drift + c_co_new = c_co.copy() + c_co_new[:,-1] += drift + """ + c_ob_new = c_ob.copy() + c_co_new = c_co.copy() + P_ob_new = P_ob.copy()#GLOBALutils.shift_P(P_ob,drift,c_ob_new,ext_aperture_ob) + P_co_new = P_co.copy()#GLOBALutils.shift_P(P_co,drift,c_co_new,ext_aperture_co) + + #plot(P_ob_new[:,1000]) + #plot(P_ob[:,1000]) + #show() + bac_fits = dirout + 'BKG_'+ fsim.split('/')[-1][:-4]+'.fits' + + if os.access(bac_fits,os.F_OK) == False: + if fsim in sim_sci: + bac = fideosutils.get_scat(data, P_ob_new + P_co_new,1) + else: + bac = fideosutils.get_scat(data, P_ob_new,1) + hdu = pyfits.PrimaryHDU(bac) + hdu.writeto(bac_fits) + else: + bac = pyfits.getdata(bac_fits) + + #imshow(bac) + #show() + #plot(data[1000]) + #plot(bac[1000]) + #show() + data -= bac + #plot(data[1000]) + #show() + sci_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.ob.fits.S' + sci_fits_co_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.co.fits.S' + bac_fits_ob_simple = dirout + fsim.split('/')[-1][:-4]+'spec.bac.ob.fits.S' + sci_fits_ob = dirout + fsim.split('/')[-1][:-4]+'spec.ob.fits.S' + sci_fits_co = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' + thar_spec_co = dirout + fsim.split('/')[-1][:-4]+'sp.co.fits' + + if ( os.access(sci_fits_ob,os.F_OK) == False ) or ( os.access(sci_fits_co,os.F_OK) == False ) or \ + ( os.access(sci_fits_ob_simple,os.F_OK) == False ) or ( os.access(sci_fits_co_simple,os.F_OK) == False ) or \ + ( force_sci_extract ): + print("No previous extraction or extraction forced for science file", fsim, "extracting...") + + sci_Ss_ob = GLOBALutils.simple_extraction(data.T,c_ob_new,ext_aperture_ob,min_extract_col,max_extract_col,npools) + bac_ob = GLOBALutils.simple_extraction(bac.T,c_ob_new,ext_aperture_ob,min_extract_col,max_extract_col,npools) + + sci_S_ob = GLOBALutils.optimal_extraction(data.T,P_ob_new,c_ob_new,ext_aperture_ob,ronoise,gain,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) + sci_Ss_ob = GLOBALutils.invert(sci_Ss_ob) + sci_S_ob = GLOBALutils.invert(sci_S_ob) + if (os.access(sci_fits_ob_simple,os.F_OK)): + os.remove( sci_fits_ob_simple ) + if (os.access(sci_fits_ob,os.F_OK)): + os.remove( sci_fits_ob ) + if (os.access(bac_fits_ob_simple,os.F_OK)): + os.remove( bac_fits_ob_simple ) + + hdu = pyfits.PrimaryHDU( sci_S_ob ) + hdu.writeto( sci_fits_ob ) + hdu = pyfits.PrimaryHDU( sci_Ss_ob ) + hdu.writeto( sci_fits_ob_simple ) + hdu = pyfits.PrimaryHDU( bac_ob ) + hdu.writeto( bac_fits_ob_simple ) + + if fsim in sim_sci: + sci_Ss_co = GLOBALutils.simple_extraction(data.T,c_co_new,ext_aperture_co,min_extract_col,max_extract_col,npools) + sci_S_co = GLOBALutils.optimal_extraction(data.T,P_co_new,c_co_new,ext_aperture_co,ronoise,gain,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) + sci_Ss_co = GLOBALutils.invert(sci_Ss_co) + sci_S_co = GLOBALutils.invert(sci_S_co) + if (os.access(sci_fits_co,os.F_OK)): + os.remove( sci_fits_co ) + if (os.access(sci_fits_co_simple,os.F_OK)): + os.remove( sci_fits_co_simple ) + hdu = pyfits.PrimaryHDU( sci_S_co ) + hdu.writeto( sci_fits_co ) + hdu = pyfits.PrimaryHDU( sci_Ss_co ) + hdu.writeto( sci_fits_co_simple ) + + else: + print(fsim, "has already been extracted, reading in product fits files...") + sci_S_ob = pyfits.getdata( sci_fits_ob ) + sci_Ss_ob = pyfits.getdata( sci_fits_ob_simple ) + bac_ob = pyfits.getdata( bac_fits_ob_simple ) + + p_shift = 0. + + if fsim in sim_sci: + sci_S_co = pyfits.getdata( sci_fits_co ) + sci_Ss_co = pyfits.getdata( sci_fits_co_simple ) + + lines_thar_co = sci_S_co[:,1,:] + iv_thar_co = sci_S_co[:,2,:] + #offset_co = GLOBALutils.get_rough_offset(lines_thar_co,reffiles) + All_Pixel_Centers_co = np.array([]) + All_Wavelengths_co = np.array([]) + All_Orders_co = np.array([]) + All_Centroids_co = np.array([]) + All_Sigmas_co = np.array([]) + All_Intensities_co = np.array([]) + All_residuals_co = np.array([]) + + order = start_co + idorder = 11 + while order <= end_co: + order_s = str(idorder) + if (idorder < 10): + order_s = '0'+str(idorder) + thar_order_orig = lines_thar_co[order,:]/scipy.signal.medfilt(S_flat_co[0].data[order,1],31) + IV = iv_thar_co[order,:] + wei = np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) #FEROSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ + = fideosutils.Initial_Wav_Calibration( order_dir+'fideos_co_'+order_s+'.iwdat', thar_order, order, wei, \ + rmsmax=400, minlines=10,FixEnds=False,Dump_Argon=False,Cheby=True,porder=ncoef_x,rough_shift = offset_co,do_xc=False,line_width=6,pixelization=True) + #psh, pix_centers, wavelengthss, rms_mss, residualss = FEROSutils.Wav_Solution_vel_shift(wsol_dict['c_p2w_c'][order-o0], \ + # pixel_centers, wavelengths, maxrms=100, minlines=30, Cheby=use_cheby) + #shifts.append((1e-6*psh[0])*299792458.0) + if (idorder == 25): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 1023, len(thar_order) ) + + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) + All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) + All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + idorder ) + All_Centroids_co = np.append( All_Centroids_co, centroids) + All_Sigmas_co = np.append( All_Sigmas_co, sigmas) + All_Intensities_co = np.append( All_Intensities_co, intensities ) + All_residuals_co = np.append( All_residuals_co, residuals ) + order+=1 + idorder += 1 + + p0 = np.zeros( npar_wsol ) + p0[0] = (25+49) * Global_ZP + + p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ + np.ones(All_Intensities_co.shape), p0, Cheby=True,\ + maxrms=MRMS, Inv=Inverse_m,minlines=minlines_glob_co,order0=49, \ + ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + #shifts = np.array(shifts) + #shifts = FEROSutils.sigma_clip(shifts) + #the_sh = np.around(shifts.mean(),1) + #error_sh = np.around(np.sqrt(np.var(shifts)/float(len(shifts)-1)),1) + #print 'Shifts (per order):', the_sh, '+-', error_sh + thar_out_co = np.zeros((2,nord_co,lines_thar_co.shape[1])) + equis = np.arange( lines_thar_co.shape[1] ) + order = start_co + idorder = 11 + out_order = 0 + while order <= end_co: + m = idorder + 49 + chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1_co,chebs,ncoef_x,ncoef_m) + thar_out_co[0,out_order,:] = WavSol + thar_out_co[1,out_order,:] = lines_thar_co[order] + order+=1 + idorder += 1 + out_order += 1 + + if os.access(thar_spec_co,os.F_OK): + os.system('rm '+ thar_spec_co) + hdu = pyfits.PrimaryHDU(thar_out_co) + hdu.writeto(thar_spec_co) + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(All_Pixel_Centers_co, All_Wavelengths_co, All_Orders_co,\ + np.ones(All_Intensities_co.shape), wsol_dict['p1_co'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob_co,\ + order0=49,ntotal=ntot,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + werr_co = rms_ms/np.sqrt(float(len(residuals))) + rv_drift_co = (1e-6*p_shift)*299792458.0 + mat_co = np.array([mjd, rv_drift_co, werr_co,exptime,1]) + + shift_pix = ref_pix*(1.+rv_drift_co/299792458.0) + + if werr_co<6: + if len(drift_out_co)==0: + drift_out_co = mat_co.copy() + else: + drift_out_co = np.vstack((drift_out_co,mat_co)) + else: + p_shift = 0. + print('\t Warning!! RMS of wavelength solution is to high, setting Instrumental drift to 0 m/s...') + + np.savetxt(drift_file_co,drift_out_co) + + fout = 'proc/'+ fsim.split('/')[-1][:-4]+'sp.fits' + spec = np.zeros((11, ntot, data.shape[0])) + hdu = pyfits.PrimaryHDU( spec ) + + hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', hd['DATE-OBS'][:10] ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', hd['DATE-OBS'][11:]) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hd['EXPTIME']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',ras) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',decs) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA DEG',ras) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC DEG',decs) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) + hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DRIFT_CO',np.around(rv_drift_co[0],1)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DRIFT_CO_E',np.around(werr_co,1)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH AIRMASS',np.around(airmass.value,3)) + hdu = GLOBALutils.update_header(hdu,'BDATE',MasterBias[0].header['DATE']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH REFWAV',thars[best_id].split('/')[-1]) + + equis = np.arange( data.shape[0] ) + out_order = 0 + order = start_ob + idorder = 11 + + while order <= end_ob: + m = idorder + 49 + chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot,npix=len(equis),nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) + spec[0,out_order,:] = GLOBALutils.ToVacuum(WavSol) + spec[1,out_order,:] = sci_S_ob[order,1, :] + #spec[1,order,:] = sci_Ss_ob[order, :] + spec[2,out_order,:] = sci_S_ob[order,2, :] + # Flat-fielded spectrum + fn = S_flat_ob_n[order,1,:] + L = np.where( fn > 0 ) + spec[3,out_order,:][L] = sci_S_ob[order,1,:][L] / S_flat_ob_n[order,1,:][L] + spec[4,out_order,:][L] = sci_S_ob[order,2,:][L] * ( S_flat_ob_n[order,1,:][L] ** 2 ) + ccoef = GLOBALutils.get_cont_single(spec[0,out_order],spec[3,out_order],spec[4,out_order],ll=1.5,lu=5,nc=3) + L = np.where( spec[1,out_order] != 0 ) + spec[5,out_order,:][L] = spec[3,out_order][L] / np.polyval(ccoef,spec[0,out_order][L]) + + nJ = np.where(np.isnan(spec[5,out_order])==True)[0] + nJ2 = np.where(np.isinf(spec[5,out_order])==True)[0] + spec[5,out_order,nJ] = 1.0 + spec[5,out_order,nJ2] = 1.0 + ratio = spec[3,out_order,:][L] / spec[5,out_order,:][L] + spec[6,out_order,:][L] = spec[4,out_order,:][L] * (ratio ** 2 ) + spec[7,out_order,:][L] = ratio + spec[8,out_order,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + bac_ob[order,:][L] / gain + 2*ext_aperture_ob*(ronoise/gain)**2 ) + II = np.where(spec[8,out_order]<0)[0] + if len(II)>0: + spec[8,out_order,II] = 0. + + rI = np.where(spec[5,out_order] > 1. + 8./spec[8,out_order]) + spec[5,out_order,rI] = 1. + rI1 = np.where(spec[5,out_order] > 10) + rI2 = np.where(spec[5,out_order] < -10) + spec[5,out_order,rI1] = 1. + spec[5,out_order,rI2] = 1. + spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) + dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + + spec[9,out_order,:][L] = spec[5,out_order,:][L] * (dlambda_dx[L] ** 1) + spec[10,out_order,:][L] = spec[6,out_order,:][L] / (dlambda_dx[L] ** 2) + + order += 1 + out_order += 1 + idorder += 1 + hdu = GLOBALutils.update_header(hdu,'SFDATE',MasterBias[0].header['DATE']) + if (os.access( dirout + fout,os.F_OK)): + os.remove( dirout + fout) + hdu.writeto( dirout + fout ) + + if DoFull: + fout_full = 'proc/'+ fsim.split('/')[-1][:-4]+'sp.full.fits' + spec_full = np.zeros((11, ntot_MgIII, data.shape[0])) + hdufull = pyfits.PrimaryHDU( spec_full ) + + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH MJD', mjd) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH MBJD', mbjd) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH SHUTTER START DATE', hd['DATE-OBS'][:10] ) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH SHUTTER START UT', hd['DATE-OBS'][11:]) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH TEXP (S)',hd['EXPTIME']) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH TARGET NAME', obname) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA',ras) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC',decs) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA DEG',ras) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC DEG',decs) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH RA BARY',ra) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH DEC BARY',dec) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH EQUINOX',2000) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS LATITUDE',latitude) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS LONGITUDE',longitude) + hdufull = GLOBALutils.update_header(hdufull,'HIERARCH OBS ALTITUDE',altitude) + #hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',airmass) + + equis = np.arange( data.shape[0] ) + out_order = 0 + order = start_ob_MgIII + idorder = 0 + + while order <= end_ob_MgIII: + #print order + m = idorder + 49 + chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=49,ntotal=ntot_MgIII,npix=len(equis),nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_MgIII'],chebs,ncoef_x,ncoef_m) + spec_full[0,out_order,:] = GLOBALutils.ToVacuum(WavSol) + spec_full[1,out_order,:] = sci_S_ob[order,1, :] + #spec[1,order,:] = sci_Ss_ob[order, :] + spec_full[2,out_order,:] = sci_S_ob[order,2, :] + # Flat-fielded spectrum + fn = S_flat_ob_n[order,1,:] + L = np.where( fn > 0 ) + spec_full[3,out_order,:][L] = sci_S_ob[order,1,:][L] / S_flat_ob_n[order,1,:][L] + spec_full[4,out_order,:][L] = sci_S_ob[order,2,:][L] * ( S_flat_ob_n[order,1,:][L] ** 2 ) + ccoef = GLOBALutils.get_cont_single(spec_full[0,out_order],spec_full[3,out_order],spec_full[4,out_order],ll=1.5,lu=5,nc=3) + L = np.where( spec_full[1,out_order] != 0 ) + spec_full[5,out_order,:][L] = spec_full[3,out_order][L] / np.polyval(ccoef,spec_full[0,out_order][L]) + + nJ = np.where(np.isnan(spec_full[5,out_order])==True)[0] + nJ2 = np.where(np.isinf(spec_full[5,out_order])==True)[0] + spec_full[5,out_order,nJ] = 1.0 + spec_full[5,out_order,nJ2] = 1.0 + ratio = spec_full[3,out_order,:][L] / spec_full[5,out_order,:][L] + spec_full[6,out_order,:][L] = spec_full[4,out_order,:][L] * (ratio ** 2 ) + spec_full[7,out_order,:][L] = ratio + spec_full[8,out_order,:][L] = ratio * S_flat_ob_n[order,1,:][L] / np.sqrt( ratio * S_flat_ob_n[order,1,:][L] / gain + bac_ob[order,:][L] / gain + 2*ext_aperture_ob*(ronoise/gain)**2 ) + II = np.where(spec_full[8,out_order]<0)[0] + if len(II)>0: + spec_full[8,out_order,II] = 0. + + rI = np.where(spec_full[5,out_order] > 1. + 8./spec_full[8,out_order]) + spec_full[5,out_order,rI] = 1. + rI1 = np.where(spec_full[5,out_order] > 10) + rI2 = np.where(spec_full[5,out_order] < -10) + spec_full[5,out_order,rI1] = 1. + spec_full[5,out_order,rI2] = 1. + spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) + dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + + spec_full[9,out_order,:][L] = spec_full[5,out_order,:][L] * (dlambda_dx[L] ** 1) + spec_full[10,out_order,:][L] = spec_full[6,out_order,:][L] / (dlambda_dx[L] ** 2) + + order += 1 + out_order += 1 + idorder += 1 + hdufull = GLOBALutils.update_header(hdufull,'SFDATE',S_flat_ob[0].header['DATE']) + if (os.access( dirout + fout_full,os.F_OK)): + os.remove( dirout + fout_full) + hdufull.writeto( dirout + fout_full ) + + + if (not JustExtract): + + if DoClass: + print('\t\tSpectral Analysis:') + # spectral analysis + # First, query SIMBAD with the object name + query_success = False + sp_type_query = 'None' + #query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) + # Now, query SIMBAD by coordinates if above not successful + #if (not query_success): + # query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') + #print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + + hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) + pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' + + if os.access(pars_file,os.F_OK) == False or force_stellar_pars: + print("\t\t\tEstimating atmospheric parameters:") + spec2 = spec.copy() + T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec2,model_path=models_path,npools=npools) + line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) + f = open(pars_file,'w') + f.write(line) + f.close() + else: + print("\t\t\tAtmospheric parameters loaded from file:") + T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) + + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) + + else: + T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + + T_eff_epoch = T_eff + logg_epoch = logg + Z_epoch = Z + vsini_epoch = vsini + vel0_epoch = vel0 + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) + + print("\t\tRadial Velocity analysis:") + # assign mask + sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') + print("\t\t\tWill use",sp_type,"mask for CCF.") + + # Read in mask + ml, mh, weight = np.loadtxt(mask,unpack=True) + ml_v = GLOBALutils.ToVacuum( ml ) + mh_v = GLOBALutils.ToVacuum( mh ) + + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS + av_m = 0.5*( ml_v + mh_v ) + ml_v -= 1.5*(av_m - ml_v) + mh_v += 1.5*(mh_v - av_m) + mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m + + #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' + + disp = GLOBALutils.get_disp(obname, reffile=reffile) + if disp == 0: + known_sigma = False + if vsini != -999 and vsini > 4.: + disp = vsini + else: + disp = 4. + else: + known_sigma = True + + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + + print('\t\t\tComputing the CCF...') + cond = True + while (cond): + # first rough correlation to find the minimum + vels, xc_full, sn, nlines_ccf, W_ccf = \ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=200,vel_step=3,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=200) + v1,x1 = vels.copy(), xc_full.copy() + + for i in range(spec.shape[1]): + I = np.where((spec[0,i]>7030) & (spec[0,i] < 7050))[0] + if len(I)>0: + W_ccf[i] = 0 + I = np.where((spec[0,i]>6920) & (spec[0,i] < 6940))[0] + if len(I)>0: + W_ccf[i] = 0 + I = np.where((spec[0,i]>4300) & (spec[0,i] < 4340))[0] + if len(I)>0: + W_ccf[i] = 0 + + #I = np.where(W_ccf!=0)[0] + #W_ccf[I] = 1. + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf, start_order=2) + # Normalize the continuum of the CCF robustly with R + yy = scipy.signal.medfilt(xc_av,11) + pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) + tck1 = scipy.interpolate.splrep(vels,pred,k=1) + xc_av_orig = xc_av.copy() + xc_av /= pred + + vel0_xc = vels[ np.argmin( xc_av ) ] + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ + xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + + xc_av_rough = xc_av + vels_rough = vels + if disp > 30: + disp = 30. + vel_width = np.maximum( 20.0, 6*disp ) + + vels, xc_full, sn, nlines_ccf, W_ccf =\ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.1,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + for i in range(spec.shape[1]): + I = np.where((spec[0,i]>7030) & (spec[0,i] < 7050))[0] + if len(I)>0: + W_ccf[i] = 0 + I = np.where((spec[0,i]>6920) & (spec[0,i] < 6940))[0] + if len(I)>0: + W_ccf[i] = 0 + I = np.where((spec[0,i]>4300) & (spec[0,i] < 4340))[0] + if len(I)>0: + W_ccf[i] = 0 + + #I = np.where(W_ccf!=0)[0] + #W_ccf[I] = 1. + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf, start_order=2) + pred = scipy.interpolate.splev(vels,tck1) + xc_av /= pred + #for i in range(xc_full.shape[1]): + # plot(v1,x1[:,i]) + # plot(vels,xc_full[:,i]) + # show() + + + if sp_type == 'M5': + moon_sig = 2.5 + elif sp_type == 'K5': + moon_sig = 3.3 + else: + moon_sig = 4.5 + + p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ + sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) + + #ldc = CoralieUtils.get_ldc(T_eff, logg, Z, 1.0, ldfile = 'lin_coe_sloan2.dat') + #p1R, ROTmodel = CoralieUtils.XC_Final_Fit_Rot( vels, xc_av, ldc = ldc, vsini = vsini ) + moonmatters = False + if (know_moon and here_moon): + moonmatters = True + ismoon = True + confused = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) + moon_flag = 1 + else: + confused = False + ismoon = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 + moon_flag = 0 + + bspan = GLOBALutils.calc_bss(vels,xc_av) + SP = bspan[0] + + #print 'Bisector span:', SP + if (not known_sigma): + disp = np.floor(p1gau[2]) + if (disp < 4.0): + disp = 4.0 + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + known_sigma = True + else: + cond = False + + xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} + + moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ + 'lunation':lunation,'mephem':mephem,'texp':hd['EXPTIME']} + + pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' + pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) + + ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' + + if not avoid_plot: + GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) + + SNR_5130 = np.median(spec[8,21,1000:1101] ) + if SNR_5130 < 1: + SNR_5130 = 1. + seeing = -999 + + if sp_type == 'G2': + if T_eff < 6000: + A = 0.06544 + B = 0.00146 + D = 0.24416 + C = 0.00181 + else: + A = 0.09821 + B = 0.00014 + D = 0.33491 + C = 0.00113 + elif sp_type == 'K5': + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + else: + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + + RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) + depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) + + RVerr2_ori = -999.0 + if depth_fact >= 1.: + RVerr2 = -999.000 + else: + if sp_type == 'G2': + depth_fact = (1 - 0.62) / (1 - depth_fact) + else: + depth_fact = (1 - 0.59) / (1 - depth_fact) + RVerr2 = RVerr * depth_fact + RVerr2 = np.sqrt(RVerr2**2+0.004**2) + #if (RVerr2 <= 0.004): + # RVerr2 = 0.004 + + #if not good_quality: + # RVerr2 = np.sqrt(0.03**2 + RVerr2**2) + + BSerr = D / float(np.round(SNR_5130)) + C + + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) + RVerr2 = np.around(RVerr2,4) + BSerr = np.around(BSerr,4) + + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) + + bjd_out = 2400000.5 + mbjd + T_eff_err = 100 + logg_err = 0.5 + Z_err = 0.5 + vsini_err = 2 + XC_min = np.abs(np.around(np.min(XCmodel),2)) + + SNR_5130 = np.around(SNR_5130) + SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) + + disp_epoch = np.around(p1gau_m[2],1) + hdu = GLOBALutils.update_header(hdu,'RV', RV) + hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) + hdu = GLOBALutils.update_header(hdu,'BS', BS) + hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) + hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) + hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) + hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) + hdu = GLOBALutils.update_header(hdu,'INST', 'FIDEOS') + hdu = GLOBALutils.update_header(hdu,'RESOL', '40000') + hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') + hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) + hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) + hdu = GLOBALutils.update_header(hdu,'DATE', str(datetime.datetime.now()).replace(' ','T')) + + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f fideos ceres 40000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %7.4f %7.4f %7.4f %s\n"%\ + (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ + hd['EXPTIME'], SNR_5130_R, airmass, shift_pix, rv_drift_co, ccf_pdf) + f_res.write(line_out) + if (os.access( dirout + fout,os.F_OK)): + os.remove( dirout + fout) + hdu.writeto( dirout + fout ) + + #else: + # print "\t\tReading spectral file from", fout + # spec = pyfits.getdata( fout ) except: - print 'Problem with', fsim + print('Problem with', fsim) os.system('rm '+ dirin+'/justdo.txt') f_res.close() -np.savetxt(drift_file,drift_out) \ No newline at end of file +np.savetxt(drift_file,drift_out) diff --git a/fideos/fideosutils.py b/fideos/fideosutils.py index 2b916e9..d000bee 100644 --- a/fideos/fideosutils.py +++ b/fideos/fideosutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use('Agg') from pylab import * @@ -6,7 +7,7 @@ from scipy import signal sys.path.append("../utils/GLOBALutils") import GLOBALutils -import os +import os import glob from astropy.io import fits as pyfits from scipy import ndimage @@ -19,117 +20,117 @@ def FileClassify(diri, log): - # define output lists - biases = [] - darks = [] - flats = [] - flats_co = [] - thar = [] - thar_co = [] - sim_sci = [] - dar_sci = [] - thart = [] - simt = [] - - f = open(log,'w') - - #Do not consider the images specified in dir+badfiles.txt - bad_files = [] - if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - - all_files = glob.glob(diri+"/*fits") - for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - - if 'bias' in archivo: - biases.append(archivo) - elif 'dark' in archivo: - darks.append(archivo) - elif 'flat' in archivo: - if '1F' in archivo: - flats_co.append(archivo) - elif '2F' in archivo: - flats.append(archivo) - elif 'ThAr' in archivo: - if '1F' in archivo: - thar_co.append(archivo) - elif '2F' in archivo: - thar.append(archivo) - thart.append(mjd_fromheader(h)[0]) - else: - if '1F' in archivo: - dar_sci.append(archivo) - else: - sim_sci.append(archivo) - simt.append(mjd_fromheader(h)[0]) - - #line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) - #f.write(line) - - thart = np.array(thart) - simt = np.array(simt) - f.close() - biases, darks, flats, flats_co, thar, thar_co, sim_sci, dar_sci = \ - np.array(biases), np.array(darks), np.array(flats), np.array(flats_co), np.array(thar), \ - np.array(thar_co), np.array(sim_sci), np.array(dar_sci) - It = np.argsort(thart) - thar = thar[It] - It = np.argsort(simt) - sim_sci = sim_sci[It] - return biases, darks, flats, flats_co, thar, thar_co, sim_sci, dar_sci + # define output lists + biases = [] + darks = [] + flats = [] + flats_co = [] + thar = [] + thar_co = [] + sim_sci = [] + dar_sci = [] + thart = [] + simt = [] + + f = open(log,'w') + + #Do not consider the images specified in dir+badfiles.txt + bad_files = [] + if os.access(diri+'bad_files.txt',os.F_OK): + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + + all_files = glob.glob(diri+"/*fits") + for archivo in all_files: + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + + if 'bias' in archivo: + biases.append(archivo) + elif 'dark' in archivo: + darks.append(archivo) + elif 'flat' in archivo: + if '1F' in archivo: + flats_co.append(archivo) + elif '2F' in archivo: + flats.append(archivo) + elif 'ThAr' in archivo: + if '1F' in archivo: + thar_co.append(archivo) + elif '2F' in archivo: + thar.append(archivo) + thart.append(mjd_fromheader(h)[0]) + else: + if '1F' in archivo: + dar_sci.append(archivo) + else: + sim_sci.append(archivo) + simt.append(mjd_fromheader(h)[0]) + + #line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) + #f.write(line) + + thart = np.array(thart) + simt = np.array(simt) + f.close() + biases, darks, flats, flats_co, thar, thar_co, sim_sci, dar_sci = \ + np.array(biases), np.array(darks), np.array(flats), np.array(flats_co), np.array(thar), \ + np.array(thar_co), np.array(sim_sci), np.array(dar_sci) + It = np.argsort(thart) + thar = thar[It] + It = np.argsort(simt) + sim_sci = sim_sci[It] + return biases, darks, flats, flats_co, thar, thar_co, sim_sci, dar_sci def make_flatOB(MasterFlat, c_co,exap=5): - nord_co = len(c_co) - flat = MasterFlat.T - img_out = flat.copy() - Centers = np.zeros((len(c_co),flat.shape[1])) - ejx = np.arange(flat.shape[1]) - for o in range(nord_co): - Centers[o,:]=scipy.polyval(c_co[o],ejx) - for x in range(flat.shape[1]): - baso = np.min(flat[:,x]) - for o in range(nord_co): - cen = np.around(Centers[o,x]) - try: - bas = np.min(flat[int(np.around(cen-3*exap)):int(np.around(cen+3*exap)),x]) - except: - bas = baso - img_out[int(np.around(cen-exap)):int(np.around(cen+exap+1)),x] = bas - return img_out + nord_co = len(c_co) + flat = MasterFlat.T + img_out = flat.copy() + Centers = np.zeros((len(c_co),flat.shape[1])) + ejx = np.arange(flat.shape[1]) + for o in range(nord_co): + Centers[o,:]=scipy.polyval(c_co[o],ejx) + for x in range(flat.shape[1]): + baso = np.min(flat[:,x]) + for o in range(nord_co): + cen = np.around(Centers[o,x]) + try: + bas = np.min(flat[int(np.around(cen-3*exap)):int(np.around(cen+3*exap)),x]) + except: + bas = baso + img_out[int(np.around(cen-exap)):int(np.around(cen+exap+1)),x] = bas + return img_out def get_flatOB(MasterFlat, MasterFlat_co): - d1 = MasterFlat[1000] - d2 = MasterFlat_co[1000] - ref2 = np.arange(len(d2)) - tck = scipy.interpolate.splrep(ref2,d2,k=3) - shfts = np.arange(-6.,6.,0.001) - ccfs = [] - for shft in shfts: - ref = ref2 + shft - t2 = scipy.interpolate.splev(ref,tck) - ccfs.append(np.sum(t2*d1)) - shft = shfts[np.argmax(ccfs)] - - new_img = MasterFlat_co.copy() - for x in range(MasterFlat_co.shape[0]): - d = MasterFlat_co[x] - ref = np.arange(len(d)) - tck = scipy.interpolate.splrep(ref,d,k=1) - new_img[x] = MasterFlat[x] - scipy.interpolate.splev(ref+shft,tck) - return new_img + d1 = MasterFlat[1000] + d2 = MasterFlat_co[1000] + ref2 = np.arange(len(d2)) + tck = scipy.interpolate.splrep(ref2,d2,k=3) + shfts = np.arange(-6.,6.,0.001) + ccfs = [] + for shft in shfts: + ref = ref2 + shft + t2 = scipy.interpolate.splev(ref,tck) + ccfs.append(np.sum(t2*d1)) + shft = shfts[np.argmax(ccfs)] + + new_img = MasterFlat_co.copy() + for x in range(MasterFlat_co.shape[0]): + d = MasterFlat_co[x] + ref = np.arange(len(d)) + tck = scipy.interpolate.splrep(ref,d,k=1) + new_img[x] = MasterFlat[x] - scipy.interpolate.splev(ref+shft,tck) + return new_img def get_data(path): d = pyfits.getdata(path) @@ -142,9 +143,9 @@ def good_orders(coef,nord,ny,nx,ext_aperture): bad_inx = [] for i in range(nord): Centers[i,:]=scipy.polyval(coef[i],ejx) - I = np.where(Centers[i,:]+ext_aperture>ny)[0] - if len(I)>0: - bad_inx.append(i) + I = np.where(Centers[i,:]+ext_aperture>ny)[0] + if len(I)>0: + bad_inx.append(i) bad_inx = np.array(bad_inx) im = np.min(bad_inx) return coef[:im], nord-len(bad_inx) @@ -162,16 +163,16 @@ def clean_orders(c_all,data,exap=5): ccens = Centers[:,medc] dist1 = ccens[-1]-ccens[-2] dist2 = ccens[-2]-ccens[-3] - + if dist1 < dist2: - print 'uno' - i = nords - 1 + print('uno') + i = nords - 1 else: - print 'dos' - i = nords - 2 + print('dos') + i = nords - 2 c_co, c_ob = [],[] while i > 0: - print i + print(i) if len(c_co) == 0: c_co = c_all[i] c_ob = c_all[i-1] @@ -180,13 +181,13 @@ def clean_orders(c_all,data,exap=5): c_ob = np.vstack((c_all[i-1],c_ob)) i -= 2 - + #for i in range(len(c_ob)): # Centers[i,:]=scipy.polyval(c_ob[i],ejx) - # plot(ejx,Centers[i,:],'r') + # plot(ejx,Centers[i,:],'r') #for i in range(len(c_co)): # Centers[i,:]=scipy.polyval(c_co[i],ejx) - # plot(ejx,Centers[i,:],'b') + # plot(ejx,Centers[i,:],'b') #show() return c_ob, c_co, len(c_ob), len(c_co) @@ -226,205 +227,205 @@ def scat_flat(d,c_ob,c_co): i = 0 while i < d.shape[1]: line = d[:,i] - refmins = [] - valmins = [] - for j in range(len(Centers_co)): - p1 = Centers_ob[j,i] - p2 = Centers_co[j,i] - minv = line[p1-10:p2+10] - refv = ejy[p1-10:p2+10] - im = np.argmin(minv) - refmins.append(refv[im]) - valmins.append(minv[im]) - refmins,valmis = np.array(refmins),np.array(valmins) - #plot(line) - #plot(refmins,valmins,'ro') - #show() - #print gfd - + refmins = [] + valmins = [] + for j in range(len(Centers_co)): + p1 = Centers_ob[j,i] + p2 = Centers_co[j,i] + minv = line[p1-10:p2+10] + refv = ejy[p1-10:p2+10] + im = np.argmin(minv) + refmins.append(refv[im]) + valmins.append(minv[im]) + refmins,valmis = np.array(refmins),np.array(valmins) + #plot(line) + #plot(refmins,valmins,'ro') + #show() + #print gfd + def get_scat(sc,ps,binning): - binning = float(binning) - #print sc.shape,ps.shape - ejex = np.arange(sc.shape[1]) - bac = sc.copy() - for col in range(sc.shape[0]): - #col = 1000 - #plot(sc[col]) - #show() - #print gfds - J = np.where(ps[:,col]==0)[0] - Z = np.zeros((sc.shape[1])) - Z[J] = 1. - #plot(sc[col]) - #plot(sc[col]*Z,linewidth=2.0) - #ncol = scipy.signal.medfilt(sc[col],11) - Z2 = np.append(Z[-1],Z[:-1]) - I = np.where((Z!=0) & (Z2==0))[0] - J = np.where((Z2!=0) & (Z==0))[0] - J = J[1:] - I = I[:-1] - points = [] - vals = [] - for i in range(len(I)): - #plot(numpy.mean(ejex[I[i]:J[i]]),numpy.median(sc[2000,I[i]:J[i]]),'ro') - points.append(np.mean(ejex[I[i]:J[i]])) - vals.append(np.median(sc[col,I[i]:J[i]])) - points,vals = np.array(points),np.array(vals) - #pi = 0 - #npoints,nvals = [],[] - #while pi < len(points)-1: - # if vals[pi]>vals[pi+1]: - # npoints.append(points[pi+1]) - # nvals.append(vals[pi+1]) - # else: - # npoints.append(points[pi]) - # nvals.append(vals[pi]) - # pi+=2 - #points,vals = np.array(npoints),np.array(nvals) - - #vals = scipy.signal.medfilt(vals,3) - #plot(points,vals,'ro') - #plot(points,vals,'r') - #plot(points,scipy.signal.medfilt(vals,3),'k') - #show() - F = np.where(vals < 10000)[0] - vals = vals[F] - points = points[F] - tck = interpolate.splrep(points,vals,k=1) - scat = interpolate.splev(ejex,tck) - - scat[:I[0]] = scat[I[0]] - #scat[J[-1]:] = 0. - #plot(scat) - bac[col] = scat - #plot(ejex,scat,'r') - #show() - bacm = signal.medfilt2d(bac,[51,1]) - #plot(bac[:,1000]) - #plot(sc[1000]) - #plot(bacm[1000]) - #show() - return bacm + binning = float(binning) + #print sc.shape,ps.shape + ejex = np.arange(sc.shape[1]) + bac = sc.copy() + for col in range(sc.shape[0]): + #col = 1000 + #plot(sc[col]) + #show() + #print gfds + J = np.where(ps[:,col]==0)[0] + Z = np.zeros((sc.shape[1])) + Z[J] = 1. + #plot(sc[col]) + #plot(sc[col]*Z,linewidth=2.0) + #ncol = scipy.signal.medfilt(sc[col],11) + Z2 = np.append(Z[-1],Z[:-1]) + I = np.where((Z!=0) & (Z2==0))[0] + J = np.where((Z2!=0) & (Z==0))[0] + J = J[1:] + I = I[:-1] + points = [] + vals = [] + for i in range(len(I)): + #plot(numpy.mean(ejex[I[i]:J[i]]),numpy.median(sc[2000,I[i]:J[i]]),'ro') + points.append(np.mean(ejex[I[i]:J[i]])) + vals.append(np.median(sc[col,I[i]:J[i]])) + points,vals = np.array(points),np.array(vals) + #pi = 0 + #npoints,nvals = [],[] + #while pi < len(points)-1: + # if vals[pi]>vals[pi+1]: + # npoints.append(points[pi+1]) + # nvals.append(vals[pi+1]) + # else: + # npoints.append(points[pi]) + # nvals.append(vals[pi]) + # pi+=2 + #points,vals = np.array(npoints),np.array(nvals) + + #vals = scipy.signal.medfilt(vals,3) + #plot(points,vals,'ro') + #plot(points,vals,'r') + #plot(points,scipy.signal.medfilt(vals,3),'k') + #show() + F = np.where(vals < 10000)[0] + vals = vals[F] + points = points[F] + tck = interpolate.splrep(points,vals,k=1) + scat = interpolate.splev(ejex,tck) + + scat[:I[0]] = scat[I[0]] + #scat[J[-1]:] = 0. + #plot(scat) + bac[col] = scat + #plot(ejex,scat,'r') + #show() + bacm = signal.medfilt2d(bac,[51,1]) + #plot(bac[:,1000]) + #plot(sc[1000]) + #plot(bacm[1000]) + #show() + return bacm def get_name(arch): - obname = pyfits.getheader(arch)['OBJECT'] - if 'HD10700' in arch: - obname = 'HD10700' - return obname + obname = pyfits.getheader(arch)['OBJECT'] + if 'HD10700' in arch: + obname = 'HD10700' + return obname + - def Initial_Wav_Calibration(filename,spec,order,wei, porder=3, rmsmax=75, minlines=10, FixEnds=True, \ Dump_Argon=False, Dump_AllLines=False, Cheby=False, rough_shift = 0.0,del_width=5.0, \ - binning=1,line_width=4, fact=1,do_xc=True,sigmai=0.7,pixelization=False,fibre_ap=2.3): - - f = open(filename).readlines() - bad_indices = np.array([]) - pixel_centers = array([]) - wavelengths = array([]) - sigmas = array([]) - centroids = array([]) - intensities = array([]) - - delta=0. - N_l = 0 - out = [] - for line in f: - if line[0]!='#': - w = line.split() - nlines = int(w[0]) - pix = float(w[1])*fact/float(binning) + delta + rough_shift - wav = float(w[2]) - if pix > 20 and pix < len(spec)-20: - sigma = sigmai * fact / float(binning) - X = np.arange(int(np.around(pix))-5,int(np.around(pix))+6) - Y = spec[X] - guess = np.array([pix-0.5*fibre_ap,np.max(Y),sigmai]) - p1 = FitFideosCompProf(X, Y, guess, fibre_ap) - - pixel_centers = np.append(pixel_centers,p1[0]) - sigmas = np.append(sigmas,p1[2]) - wavelengths = np.append(wavelengths,wav) - intensities = np.append(intensities,p1[1]) - centroids = np.append(centroids, p1[0]) - N_l += 1 - - #show() - pixel_centers2 = np.around(pixel_centers).astype('int') - I = np.where((pixel_centers2>=0) & (pixel_centers20) & (pixel_centers<2048))[0] - #plot(np.around(pixel_centers[I]).astype('int'),spec[np.around(pixel_centers[I]).astype('int')],'ro') - #show() - I1 = np.where(pixel_centers<50)[0] - I2 = np.where(pixel_centers>len(spec)-50)[0] - II = np.hstack((I1,I2)) - bad_indices = np.hstack((np.array(bad_indices),II)) - bad_indices = list(np.unique(bad_indices)) - # now, do the polynomial fit, rejecting some lines until RMS is below rmsmax - I = range( N_l ) - - for bi in bad_indices: - I.remove( bi ) - N_l -= 1 - - if (Cheby): - coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) - coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) - else: - coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) - coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) - - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - - if (FixEnds): - minI = np.min( I ) + 1 - maxI = np.max( I ) - 1 - else: - minI = np.min( I ) - maxI = np.max( I ) - #if order==26: - # plot(pixel_centers[I],residuals,'ro') - # plot([0,4096],[0,0]) - #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) - #show() - #print dfgh - count = 0 - while ((N_l > minlines) and (rmsms > rmsmax)): - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - index_worst = np.argmax( np.absolute(residuals) ) - I.pop( index_worst) - N_l -= 1 - if (Cheby): - coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) - coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) - else: - coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) - coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) - count +=1 - - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - - pci = np.around(pixel_centers).astype('int') - #plot(spec) - #plot(pci,spec[pci],'ro') - #plot(pci[I],spec[pci[I]],'bo') - #show() - #plot(wavelengths[I],residuals,'ro') - #show() - #print "RMS is ", rmsms, "using ", N_l, " lines at indices ", I - #plot(pixel_centers[I],wavelengths[I],'ro') - #if order == 26: - # plot(pixel_centers[I],residuals-0.1,'bo') - # plot([0,4096],[-0.1,-0.1]) - # #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) - # show() - #print order, len(pixel_centers), len(I) - return coeffs_pix2wav, coeffs_pix2sigma, pixel_centers[I], wavelengths[I], \ - rmsms, residuals, centroids[I], sigmas[I], intensities[I] + binning=1,line_width=4, fact=1,do_xc=True,sigmai=0.7,pixelization=False,fibre_ap=2.3): + + f = open(filename).readlines() + bad_indices = np.array([]) + pixel_centers = array([]) + wavelengths = array([]) + sigmas = array([]) + centroids = array([]) + intensities = array([]) + + delta=0. + N_l = 0 + out = [] + for line in f: + if line[0]!='#': + w = line.split() + nlines = int(w[0]) + pix = float(w[1])*fact/float(binning) + delta + rough_shift + wav = float(w[2]) + if pix > 20 and pix < len(spec)-20: + sigma = sigmai * fact / float(binning) + X = np.arange(int(np.around(pix))-5,int(np.around(pix))+6) + Y = spec[X] + guess = np.array([pix-0.5*fibre_ap,np.max(Y),sigmai]) + p1 = FitFideosCompProf(X, Y, guess, fibre_ap) + + pixel_centers = np.append(pixel_centers,p1[0]) + sigmas = np.append(sigmas,p1[2]) + wavelengths = np.append(wavelengths,wav) + intensities = np.append(intensities,p1[1]) + centroids = np.append(centroids, p1[0]) + N_l += 1 + + #show() + pixel_centers2 = np.around(pixel_centers).astype('int') + I = np.where((pixel_centers2>=0) & (pixel_centers20) & (pixel_centers<2048))[0] + #plot(np.around(pixel_centers[I]).astype('int'),spec[np.around(pixel_centers[I]).astype('int')],'ro') + #show() + I1 = np.where(pixel_centers<50)[0] + I2 = np.where(pixel_centers>len(spec)-50)[0] + II = np.hstack((I1,I2)) + bad_indices = np.hstack((np.array(bad_indices),II)) + bad_indices = list(np.unique(bad_indices)) + # now, do the polynomial fit, rejecting some lines until RMS is below rmsmax + I = range( N_l ) + + for bi in bad_indices: + I.remove( bi ) + N_l -= 1 + + if (Cheby): + coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) + coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) + else: + coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) + coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) + + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + + if (FixEnds): + minI = np.min( I ) + 1 + maxI = np.max( I ) - 1 + else: + minI = np.min( I ) + maxI = np.max( I ) + #if order==26: + # plot(pixel_centers[I],residuals,'ro') + # plot([0,4096],[0,0]) + #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) + #show() + #print dfgh + count = 0 + while ((N_l > minlines) and (rmsms > rmsmax)): + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + index_worst = np.argmax( np.absolute(residuals) ) + I.pop( index_worst) + N_l -= 1 + if (Cheby): + coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) + coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) + else: + coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) + coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) + count +=1 + + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + + pci = np.around(pixel_centers).astype('int') + #plot(spec) + #plot(pci,spec[pci],'ro') + #plot(pci[I],spec[pci[I]],'bo') + #show() + #plot(wavelengths[I],residuals,'ro') + #show() + #print "RMS is ", rmsms, "using ", N_l, " lines at indices ", I + #plot(pixel_centers[I],wavelengths[I],'ro') + #if order == 26: + # plot(pixel_centers[I],residuals-0.1,'bo') + # plot([0,4096],[-0.1,-0.1]) + # #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) + # show() + #print order, len(pixel_centers), len(I) + return coeffs_pix2wav, coeffs_pix2sigma, pixel_centers[I], wavelengths[I], \ + rmsms, residuals, centroids[I], sigmas[I], intensities[I] def rms_ms(coeffs_pix2wav, pixel_centers, wavelengths, npix, Cheby=False): " Returns rms deviation of best fit in m/s" @@ -437,79 +438,79 @@ def rms_ms(coeffs_pix2wav, pixel_centers, wavelengths, npix, Cheby=False): central_wav = 0.5 * (scipy.polyval(coeffs_pix2wav,50.) + scipy.polyval(coeffs_pix2wav,npix-50)) rms_ms = np.sqrt( np.var( residuals ) ) * 299792458.0 / central_wav - + return rms_ms, residuals def fit2d(sc,trace,filename,spec,order,wei, porder=3, rmsmax=75, minlines=10, FixEnds=True, \ Dump_Argon=False, Dump_AllLines=False, Cheby=False, rough_shift = 0.0,del_width=5.0, \ - binning=1,line_width=4, fact=1,do_xc=True,sigmai=0.8,pixelization=False): - - f = open(filename).readlines() - - pixel_centers = array([]) - wavelengths = array([]) - sigmas = array([]) - centroids = array([]) - intensities = array([]) - - delta=0. - N_l = 0 - out = [] - for line in f: - if line[0]!='#': - w = line.split() - nlines = int(w[0]) - pix = float(w[1])*fact/float(binning) + delta + rough_shift - wav = float(w[2]) - if pix > 20 and pix < len(spec)-20: - #X = array(range(pix-line_width,pix+line_width+1)) - #Y = spec[pix-line_width:pix+line_width+1] - sigma = sigmai * fact / float(binning) - #p1 = FitFideosCompProf(X, Y, B, mu[0], sigma[0]) - #print trace - y1 = 2064 - np.polyval(trace,2048.-pix) - #print '#',2048-pix,y1 - - X = np.arange(int(np.around(2048.-pix))-5,int(np.around(2048-pix))+5) - Y = np.arange(int(np.around(y1))-5,int(np.around(y1))+6) - - F = sc.T - F = F[X,:] - F = F[:,Y] - B1 = F[0] - B2 = F[-1] - B3 = F[:,0] - B4 = F[:,-1] - BAC = np.median(np.hstack((B1,B2,B3,B4))) - F = F - BAC - Ferr = np.sqrt(np.sqrt(np.absolute(F))**2 + 9.) - #pars = np.array([1.8128e+03, 1.7167e+03, 7.500e+03, 2.3, 6.15]) - #model = FideosCompProf(pars,X,Y) - #model2 = FideosCompProf(np.array([2048-pix, y1,F.max()]),X,Y) - #for i in range(F.shape[1]): - # errorbar(X,F[:,i],yerr=Ferr[:,i],fmt='b') - # plot(X,model[:,i],'r') - # plot(Y,model2[i],'k') - #show() - - #print X,Y,pix,y1 - #p1 = FitFideos2DProf(X, Y, F, 2048-pix, y1) - guess = np.array([2048-pix, y1,F.max(),2.,10.]) - ndim = len(guess) - nwalkers=100 - pos = [guess + np.random.randn(ndim)*np.array([2,2,500.,2.,4.]) for i in range(nwalkers)] - sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(X,Y,F,Ferr)) - sampler.run_mcmc(pos, 500) - samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) - #print samples.shape - print np.median(samples,axis=0) - if len(out) == 0: - out = np.median(samples,axis=0) - else: - out = np.vstack((out,np.median(samples,axis=0))) - #fig = corner.corner(samples, labels=["$Xpos$", "$Ypos$", "Amp", 'Fibwidth', 'Inst']) - #show() - return out + binning=1,line_width=4, fact=1,do_xc=True,sigmai=0.8,pixelization=False): + + f = open(filename).readlines() + + pixel_centers = array([]) + wavelengths = array([]) + sigmas = array([]) + centroids = array([]) + intensities = array([]) + + delta=0. + N_l = 0 + out = [] + for line in f: + if line[0]!='#': + w = line.split() + nlines = int(w[0]) + pix = float(w[1])*fact/float(binning) + delta + rough_shift + wav = float(w[2]) + if pix > 20 and pix < len(spec)-20: + #X = array(range(pix-line_width,pix+line_width+1)) + #Y = spec[pix-line_width:pix+line_width+1] + sigma = sigmai * fact / float(binning) + #p1 = FitFideosCompProf(X, Y, B, mu[0], sigma[0]) + #print trace + y1 = 2064 - np.polyval(trace,2048.-pix) + #print '#',2048-pix,y1 + + X = np.arange(int(np.around(2048.-pix))-5,int(np.around(2048-pix))+5) + Y = np.arange(int(np.around(y1))-5,int(np.around(y1))+6) + + F = sc.T + F = F[X,:] + F = F[:,Y] + B1 = F[0] + B2 = F[-1] + B3 = F[:,0] + B4 = F[:,-1] + BAC = np.median(np.hstack((B1,B2,B3,B4))) + F = F - BAC + Ferr = np.sqrt(np.sqrt(np.absolute(F))**2 + 9.) + #pars = np.array([1.8128e+03, 1.7167e+03, 7.500e+03, 2.3, 6.15]) + #model = FideosCompProf(pars,X,Y) + #model2 = FideosCompProf(np.array([2048-pix, y1,F.max()]),X,Y) + #for i in range(F.shape[1]): + # errorbar(X,F[:,i],yerr=Ferr[:,i],fmt='b') + # plot(X,model[:,i],'r') + # plot(Y,model2[i],'k') + #show() + + #print X,Y,pix,y1 + #p1 = FitFideos2DProf(X, Y, F, 2048-pix, y1) + guess = np.array([2048-pix, y1,F.max(),2.,10.]) + ndim = len(guess) + nwalkers=100 + pos = [guess + np.random.randn(ndim)*np.array([2,2,500.,2.,4.]) for i in range(nwalkers)] + sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(X,Y,F,Ferr)) + sampler.run_mcmc(pos, 500) + samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) + #print samples.shape + print(np.median(samples,axis=0)) + if len(out) == 0: + out = np.median(samples,axis=0) + else: + out = np.vstack((out,np.median(samples,axis=0))) + #fig = corner.corner(samples, labels=["$Xpos$", "$Ypos$", "Amp", 'Fibwidth', 'Inst']) + #show() + return out def rms_ms(coeffs_pix2wav, pixel_centers, wavelengths, npix, Cheby=False): " Returns rms deviation of best fit in m/s" @@ -522,7 +523,7 @@ def rms_ms(coeffs_pix2wav, pixel_centers, wavelengths, npix, Cheby=False): central_wav = 0.5 * (scipy.polyval(coeffs_pix2wav,50.) + scipy.polyval(coeffs_pix2wav,npix-50)) rms_ms = np.sqrt( np.var( residuals ) ) * 299792458.0 / central_wav - + return rms_ms, residuals def Cheby_Fit(x,y,order,npix): @@ -540,7 +541,7 @@ def fitfunc(p,chebs,order): ret_val += p[order-i]*chebs[i] return ret_val errfunc = lambda p,chebs,y,order: np.ravel( (fitfunc(p,chebs,order)-y) ) - + def get_chebs(x,order): chebs = [] for i in range(0,order+1): @@ -563,16 +564,16 @@ def Cheby_eval(p,x,npix): ret_val = 0.0 for i in range(order + 1): ret_val += p[order - i]*scipy.special.chebyt(i)(x_norm) - + return ret_val def LineFit_SingleSigma(X, Y, B, mu, sigma, weight,pixelization=False): """ This function fits a series of Gaussians simultaneously, given a set of input pixels, sigmas, and intensities - + Sigma is the same for all lines - + Returns (mu_i, sigm_i, int_i), i=1,.,n where n is the number of components """ @@ -581,18 +582,18 @@ def LineFit_SingleSigma(X, Y, B, mu, sigma, weight,pixelization=False): def fitfunc(p, x, n): - if pixelization: - lxo = len(x) - xo = x.copy() - x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) + if pixelization: + lxo = len(x) + xo = x.copy() + x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) ret = np.zeros(len(x)) for i in range(n): ret += ( p[i*2+1] * IntGaussian(x,p[i*2+2],p[0]) ) - if pixelization: - ret = ret.reshape((lxo,100)) - ret = np.mean(ret,axis=1) + if pixelization: + ret = ret.reshape((lxo,100)) + ret = np.mean(ret,axis=1) return ret @@ -600,7 +601,7 @@ def fitfunc(p, x, n): # Build error array #err = sqrt((B+Y)/gain + readnoise**2) - + # Build input parameters list # length of inout parameter list: 3 * n p0 = np.zeros(2*n+1) @@ -611,7 +612,7 @@ def fitfunc(p, x, n): # perform fit #plot(X,Y,'b') - + p1, success = scipy.optimize.leastsq(errfunc,p0, args=(X,n,Y-B, weight)) #plot(X,fitfunc(p1,X,n),'r') # build output consistent with LineFit @@ -621,33 +622,33 @@ def fitfunc(p, x, n): p_output[i*3+1] = p1[i*2 + 2] p_output[i*3+2] = p1[0] - return p_output, success + return p_output, success def TruncGaussian(x,y,x0,y0,dev): - if x > x0: - return 0 - elif x < x0 - 3*dev: - return 0 - c1 = (x - x0)**2/(2.*dev*dev) - c2 = (y - y0)**2/(2.*dev*dev) - z = np.exp(-(c1+c2)) - return z + if x > x0: + return 0 + elif x < x0 - 3*dev: + return 0 + c1 = (x - x0)**2/(2.*dev*dev) + c2 = (y - y0)**2/(2.*dev*dev) + z = np.exp(-(c1+c2)) + return z def HalfFiber(x,y,x0,y0,lim): - if x > x0: - return 0. - if np.sqrt((x-x0)**2 + (y-y0)**2) x0: + return 0. + if np.sqrt((x-x0)**2 + (y-y0)**2)=X[x]-0.5)&(Xt=Y[y]-0.5)&(Yt=X[x]-0.5)&(Xt=Y[y]-0.5)&(Yt=X[x]-0.5)&(Xt=Y[y]-0.5)&(Yt=X[x]-0.5)&(Xt=Y[y]-0.5)&(Yt 2457661.5 - 2400000.5: - sufix = '.new.iwdat' - oii = 100 - off = 2048 + sufix = '.new.iwdat' + oii = 100 + off = 2048 -print '\tThese are all the images to proccess:' +print('\tThese are all the images to proccess:') f = open(log) flines = f.readlines() for line in flines: - print '\t'+line[:-1] -print '\n' + print('\t'+line[:-1]) +print('\n') if ( (os.access(dirout+'Flat.fits',os.F_OK) == False) or \ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): - # median combine Biases - print "\tGenerating Master calibration frames..." - if len(biases)>0: - MasterBias, RO_bias, GA_bias = fiesutils.MedianCombine(biases, binning=binning, off=off, oii=oii) - else: - MasterBias = np.zeros(fiesutils.OverscanTrim(pyfits.getdata(flats[0]),binning=binning).shape,ii=oii,ff=off) - RO_bias = 0. - GA_bias = 1. - print "\t\t-> Masterbias: done!" - hdu = pyfits.PrimaryHDU( MasterBias ) - if (os.access(dirout+'MasterBias.fits',os.F_OK)): - os.remove(dirout+'MasterBias.fits') - hdu.writeto(dirout+'MasterBias.fits') - - dark_names = [] - dark_utimes = [] - if dark_corr and len(darks)>0: - dark_utimes, dark_times = fiesutils.get_darktimes(darks) - for tim in dark_utimes: - I = np.where(dark_times == tim)[0] - dark,ron_d,gain_d = fiesutils.MedianCombine(darks[I], zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) - hdu = pyfits.PrimaryHDU(dark) - dark_names.append(dirout+'Dark_'+str(int(tim))+'.fits') - if (os.access(dark_names[-1],os.F_OK)): - os.remove(dark_names[-1]) - hdu.writeto(dark_names[-1]) - print "\t\t-> MasterDarks: done!" - dark_names, dark_utimes = np.array(dark_names), np.array(dark_utimes) - - # median combine list of flats - Flat, RO_fl, GA_fl = fiesutils.MedianCombine(flats, zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) - hdu = pyfits.PrimaryHDU(Flat) - if (os.access(dirout+'Flat.fits',os.F_OK)): - os.remove(dirout+'Flat.fits') - hdu.writeto(dirout+'Flat.fits') - - - if len(flats_co)>0: - Flat_co, RO_fl_co, GA_fl_co = fiesutils.MedianCombine(flats_co, zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) - hdu = pyfits.PrimaryHDU(Flat_co) - if (os.access(dirout+'Flat_co.fits',os.F_OK)): - os.remove(dirout+'Flat_co.fits') - hdu.writeto(dirout+'Flat_co.fits') - else: - c_co = [] - nord_co = 0 - print "\t\t-> Masterflats: done!" - - print "\tTracing echelle orders..." - c_all,nord = GLOBALutils.get_them(Flat.T, ext_aperture, trace_degree, maxords=-1, mode=1,nsigmas=5.) - c_all,nord = GLOBALutils.good_orders(c_all,nord,Flat.shape[1],Flat.shape[0],ext_aperture) - print '\t\t'+ str(nord)+' orders found.' - if len(flats_co)>0: - c_co, nord_co = GLOBALutils.get_them(Flat_co.T, ext_aperture, trace_degree, maxords=-1, mode=1,nsigmas=5.) - c_co, nord_co = GLOBALutils.good_orders(c_co,nord_co,Flat_co.shape[1],Flat_co.shape[0],ext_aperture) - print '\t\t'+ str(nord_co)+' comparison orders found.' - # pickle traces - trace_dict = {'c_all':c_all, 'c_co':c_co, - 'nord':nord, 'nord_co':nord_co, - 'GA_bias': GA_bias, 'RO_bias' : RO_bias, - 'GA_fl': GA_fl, 'RO_fl': RO_fl, - 'dark_names':dark_names, 'dark_utimes':dark_utimes} - pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) + # median combine Biases + print("\tGenerating Master calibration frames...") + if len(biases)>0: + MasterBias, RO_bias, GA_bias = fiesutils.MedianCombine(biases, binning=binning, off=off, oii=oii) + else: + MasterBias = np.zeros(fiesutils.OverscanTrim(pyfits.getdata(flats[0]),binning=binning).shape,ii=oii,ff=off) + RO_bias = 0. + GA_bias = 1. + print("\t\t-> Masterbias: done!") + hdu = pyfits.PrimaryHDU( MasterBias ) + if (os.access(dirout+'MasterBias.fits',os.F_OK)): + os.remove(dirout+'MasterBias.fits') + hdu.writeto(dirout+'MasterBias.fits') + + dark_names = [] + dark_utimes = [] + if dark_corr and len(darks)>0: + dark_utimes, dark_times = fiesutils.get_darktimes(darks) + for tim in dark_utimes: + I = np.where(dark_times == tim)[0] + dark,ron_d,gain_d = fiesutils.MedianCombine(darks[I], zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) + hdu = pyfits.PrimaryHDU(dark) + dark_names.append(dirout+'Dark_'+str(int(tim))+'.fits') + if (os.access(dark_names[-1],os.F_OK)): + os.remove(dark_names[-1]) + hdu.writeto(dark_names[-1]) + print("\t\t-> MasterDarks: done!") + dark_names, dark_utimes = np.array(dark_names), np.array(dark_utimes) + + # median combine list of flats + Flat, RO_fl, GA_fl = fiesutils.MedianCombine(flats, zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) + hdu = pyfits.PrimaryHDU(Flat) + if (os.access(dirout+'Flat.fits',os.F_OK)): + os.remove(dirout+'Flat.fits') + hdu.writeto(dirout+'Flat.fits') + + + if len(flats_co)>0: + Flat_co, RO_fl_co, GA_fl_co = fiesutils.MedianCombine(flats_co, zero=dirout+'MasterBias.fits',binning=binning, off=off, oii=oii) + hdu = pyfits.PrimaryHDU(Flat_co) + if (os.access(dirout+'Flat_co.fits',os.F_OK)): + os.remove(dirout+'Flat_co.fits') + hdu.writeto(dirout+'Flat_co.fits') + else: + c_co = [] + nord_co = 0 + print("\t\t-> Masterflats: done!") + + print("\tTracing echelle orders...") + c_all,nord = GLOBALutils.get_them(Flat.T, ext_aperture, trace_degree, maxords=-1, mode=1,nsigmas=5.) + c_all,nord = GLOBALutils.good_orders(c_all,nord,Flat.shape[1],Flat.shape[0],ext_aperture) + print('\t\t'+ str(nord)+' orders found.') + if len(flats_co)>0: + c_co, nord_co = GLOBALutils.get_them(Flat_co.T, ext_aperture, trace_degree, maxords=-1, mode=1,nsigmas=5.) + c_co, nord_co = GLOBALutils.good_orders(c_co,nord_co,Flat_co.shape[1],Flat_co.shape[0],ext_aperture) + print('\t\t'+ str(nord_co)+' comparison orders found.') + # pickle traces + trace_dict = {'c_all':c_all, 'c_co':c_co, + 'nord':nord, 'nord_co':nord_co, + 'GA_bias': GA_bias, 'RO_bias' : RO_bias, + 'GA_fl': GA_fl, 'RO_fl': RO_fl, + 'dark_names':dark_names, 'dark_utimes':dark_utimes} + pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) else: - trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) - c_all = trace_dict['c_all'] - nord = trace_dict['nord'] - c_co = trace_dict['c_co'] - nord_co = trace_dict['nord_co'] - # recover GA*, RO* - GA_bias = trace_dict['GA_bias'] - RO_bias = trace_dict['RO_bias'] - GA_fl = trace_dict['GA_fl'] - RO_fl = trace_dict['RO_fl'] - if dark_corr and len(darks)>0: - dark_utimes = trace_dict['dark_utimes'] - dark_names = trace_dict['dark_names'] - # recover flats & master bias - h = pyfits.open(dirout+'Flat.fits') - Flat = h[0].data - if len(c_co)>0: - h = pyfits.open(dirout+'Flat_co.fits') - Flat_co = h[0].data - h = pyfits.open(dirout+'MasterBias.fits') - MasterBias = h[0].data - -print '\n\tExtraction of Flat calibration frames:' + trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) + c_all = trace_dict['c_all'] + nord = trace_dict['nord'] + c_co = trace_dict['c_co'] + nord_co = trace_dict['nord_co'] + # recover GA*, RO* + GA_bias = trace_dict['GA_bias'] + RO_bias = trace_dict['RO_bias'] + GA_fl = trace_dict['GA_fl'] + RO_fl = trace_dict['RO_fl'] + if dark_corr and len(darks)>0: + dark_utimes = trace_dict['dark_utimes'] + dark_names = trace_dict['dark_names'] + # recover flats & master bias + h = pyfits.open(dirout+'Flat.fits') + Flat = h[0].data + if len(c_co)>0: + h = pyfits.open(dirout+'Flat_co.fits') + Flat_co = h[0].data + h = pyfits.open(dirout+'MasterBias.fits') + MasterBias = h[0].data + +print('\n\tExtraction of Flat calibration frames:') if len(c_co)>0: - c_tot = GLOBALutils.Mesh(c_all,c_co) + c_tot = GLOBALutils.Mesh(c_all,c_co) P_fits = dirout + 'P.fits' S_flat_fits = dirout +'flat.fits' S_flat = np.zeros((nord, 3, Flat.shape[1]) ) @@ -275,7 +276,7 @@ if ( os.access(P_fits,os.F_OK) == False ) or \ ( os.access(S_flat_fits,os.F_OK) == False ) or \ (force_flat_extract): - print "\t\tNo extracted flat object spectra found or extraction forced, extracting and saving..." + print("\t\tNo extracted flat object spectra found or extraction forced, extracting and saving...") Centers = np.zeros((len(c_all),Flat.shape[0])) for i in range(nord): @@ -285,11 +286,11 @@ fl = Flat.T - bac bacfile = dirout + 'BAC_FLAT.fits' if (os.access(bacfile,os.F_OK)): - os.remove( bacfile ) + os.remove( bacfile ) hdbac = pyfits.PrimaryHDU( bac ) hdbac.writeto(bacfile) - print "\t\tWill extract",nord,"orders for object fibre..." + print("\t\tWill extract",nord,"orders for object fibre...") P = GLOBALutils.obtain_P(fl,c_all,ext_aperture,RO_fl,\ GA_fl,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_col,\ @@ -310,675 +311,675 @@ hdu.writeto( S_flat_fits ) else: - print "\t\tExtracted flat object spectra found, loading..." + print("\t\tExtracted flat object spectra found, loading...") P = pyfits.getdata( P_fits ) S_flat = pyfits.getdata( S_flat_fits ) if nord_co>0: - P_co_fits = dirout + 'P_co.fits' - S_flat_co_fits = dirout +'flat_co.fits' - S_flat_co = np.zeros((nord_co, 3, Flat_co.shape[1]) ) - - if ( os.access(P_co_fits,os.F_OK) == False ) or \ - ( os.access(S_flat_co_fits,os.F_OK) == False ) or \ - (force_flat_extract): - print "\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving..." - - Centers = np.zeros((len(c_co),Flat_co.shape[0])) - for i in range(nord_co): - Centers[i,:]=scipy.polyval(c_co[i,:],np.arange(len(Centers[i,:]))) - - bac = GLOBALutils.get_scat(Flat_co.T,Centers,span=bacap) - fl = Flat_co.T - bac - bacfile = dirout + 'BAC_FLAT_CO.fits' - if (os.access(bacfile,os.F_OK)): - os.remove( bacfile ) - hdbac = pyfits.PrimaryHDU( bac ) - hdbac.writeto(bacfile) - - print "\t\tWill extract",nord_co,"orders for comparison fibre..." - P_co = GLOBALutils.obtain_P(fl,c_co,ext_aperture,RO_fl,\ - GA_fl,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) - - if (os.access(P_co_fits,os.F_OK)): - os.remove( P_co_fits ) - hdu = pyfits.PrimaryHDU( P_co ) - hdu.writeto( P_co_fits ) - - S_flat_co = GLOBALutils.optimal_extraction(fl,P_co,c_co,ext_aperture,RO_fl,GA_fl,\ - S_Marsh,10*NCosmic_Marsh,min_extract_col,max_extract_col,npools) - S_flat_co = S_flat_co[::-1] - S_flat_co = GLOBALutils.invert(S_flat_co) - if (os.access(S_flat_co_fits,os.F_OK)): - os.remove( S_flat_co_fits ) - hdu = pyfits.PrimaryHDU( S_flat_co ) - hdu.writeto( S_flat_co_fits ) - - else: - print "\t\tExtracted flat object spectra found, loading..." - P_co = pyfits.getdata( P_co_fits ) - S_flat_co = pyfits.getdata( S_flat_co_fits ) + P_co_fits = dirout + 'P_co.fits' + S_flat_co_fits = dirout +'flat_co.fits' + S_flat_co = np.zeros((nord_co, 3, Flat_co.shape[1]) ) + + if ( os.access(P_co_fits,os.F_OK) == False ) or \ + ( os.access(S_flat_co_fits,os.F_OK) == False ) or \ + (force_flat_extract): + print("\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving...") + + Centers = np.zeros((len(c_co),Flat_co.shape[0])) + for i in range(nord_co): + Centers[i,:]=scipy.polyval(c_co[i,:],np.arange(len(Centers[i,:]))) + + bac = GLOBALutils.get_scat(Flat_co.T,Centers,span=bacap) + fl = Flat_co.T - bac + bacfile = dirout + 'BAC_FLAT_CO.fits' + if (os.access(bacfile,os.F_OK)): + os.remove( bacfile ) + hdbac = pyfits.PrimaryHDU( bac ) + hdbac.writeto(bacfile) + + print("\t\tWill extract",nord_co,"orders for comparison fibre...") + P_co = GLOBALutils.obtain_P(fl,c_co,ext_aperture,RO_fl,\ + GA_fl,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) + + if (os.access(P_co_fits,os.F_OK)): + os.remove( P_co_fits ) + hdu = pyfits.PrimaryHDU( P_co ) + hdu.writeto( P_co_fits ) + + S_flat_co = GLOBALutils.optimal_extraction(fl,P_co,c_co,ext_aperture,RO_fl,GA_fl,\ + S_Marsh,10*NCosmic_Marsh,min_extract_col,max_extract_col,npools) + S_flat_co = S_flat_co[::-1] + S_flat_co = GLOBALutils.invert(S_flat_co) + if (os.access(S_flat_co_fits,os.F_OK)): + os.remove( S_flat_co_fits ) + hdu = pyfits.PrimaryHDU( S_flat_co ) + hdu.writeto( S_flat_co_fits ) + + else: + print("\t\tExtracted flat object spectra found, loading...") + P_co = pyfits.getdata( P_co_fits ) + S_flat_co = pyfits.getdata( S_flat_co_fits ) # Normalize flat field spectra. S_flat_n, Snorms = GLOBALutils.FlatNormalize_single( S_flat, mid = int(.5*S_flat.shape[2]) ) if nord_co>0: - S_flat_co_n, Snorms_co = GLOBALutils.FlatNormalize_single( S_flat_co, mid = int(.5*S_flat_co.shape[2]) ) + S_flat_co_n, Snorms_co = GLOBALutils.FlatNormalize_single( S_flat_co, mid = int(.5*S_flat_co.shape[2]) ) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in ThAr_ref: - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' - thar_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' - if ( os.access(thar_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): - hthar = pyfits.open( fsim ) - dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias - Centers = np.zeros((len(c_all),dthar.shape[0])) - for i in range(nord): - Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) - bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture,option=1,allow_neg=True) - sdthar = dthar.T - bac - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." - thar_S = np.zeros( (nord,3,dthar.shape[0]) ) - thar_Ss = np.zeros( (nord,dthar.shape[0]) ) - - tR,tG = fiesutils.get_RONGAIN(hthar[1].header) - - thar_Ss = GLOBALutils.simple_extraction(sdthar,c_all,ext_aperture,\ - min_extract_col,max_extract_col,npools) - thar_Ss = thar_Ss[::-1] - thar_Ss = GLOBALutils.invert(thar_Ss) - if (os.access(thar_fits,os.F_OK)): - os.remove( thar_fits ) - if (os.access(thar_simple_fits,os.F_OK)): - os.remove( thar_simple_fits ) - - hdu = pyfits.PrimaryHDU( thar_S ) - hdu.writeto( thar_fits ) - hdu = pyfits.PrimaryHDU( thar_Ss ) - hdu.writeto( thar_simple_fits ) - else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' + thar_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' + if ( os.access(thar_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): + hthar = pyfits.open( fsim ) + dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias + Centers = np.zeros((len(c_all),dthar.shape[0])) + for i in range(nord): + Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) + bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture,option=1,allow_neg=True) + sdthar = dthar.T - bac + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") + thar_S = np.zeros( (nord,3,dthar.shape[0]) ) + thar_Ss = np.zeros( (nord,dthar.shape[0]) ) + + tR,tG = fiesutils.get_RONGAIN(hthar[1].header) + + thar_Ss = GLOBALutils.simple_extraction(sdthar,c_all,ext_aperture,\ + min_extract_col,max_extract_col,npools) + thar_Ss = thar_Ss[::-1] + thar_Ss = GLOBALutils.invert(thar_Ss) + if (os.access(thar_fits,os.F_OK)): + os.remove( thar_fits ) + if (os.access(thar_simple_fits,os.F_OK)): + os.remove( thar_simple_fits ) + + hdu = pyfits.PrimaryHDU( thar_S ) + hdu.writeto( thar_fits ) + hdu = pyfits.PrimaryHDU( thar_Ss ) + hdu.writeto( thar_simple_fits ) + else: + print("\t\tThAr file", fsim, "all ready extracted, loading...") # Extract all ThAr files for fsim in ThAr_co: - thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' - thar_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' - if ( os.access(thar_co_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): - hthar = pyfits.open( fsim ) - dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias - Centers = np.zeros((len(c_co),dthar.shape[0])) - for i in range(nord_co): - Centers[i,:]=scipy.polyval(c_co[i,:],np.arange(len(Centers[i,:]))) - bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture+2) - sdthar = dthar.T - bac - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." - thar_S = np.zeros( (nord_co,3,dthar.shape[0]) ) - thar_Ss = np.zeros( (nord_co,dthar.shape[0]) ) - - tR,tG = fiesutils.get_RONGAIN(hthar[1].header) - #print tR,tG - thar_Ss = GLOBALutils.simple_extraction(sdthar,c_co,ext_aperture,\ - min_extract_col,max_extract_col,npools) - thar_Ss = thar_Ss[::-1] - thar_Ss = GLOBALutils.invert(thar_Ss) - if (os.access(thar_co_fits,os.F_OK)): - os.remove( thar_co_fits ) - if (os.access(thar_co_simple_fits,os.F_OK)): - os.remove( thar_co_simple_fits ) - - hdu = pyfits.PrimaryHDU( thar_S ) - hdu.writeto( thar_co_fits ) - hdu = pyfits.PrimaryHDU( thar_Ss ) - hdu.writeto( thar_co_simple_fits ) - else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' + thar_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' + if ( os.access(thar_co_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): + hthar = pyfits.open( fsim ) + dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias + Centers = np.zeros((len(c_co),dthar.shape[0])) + for i in range(nord_co): + Centers[i,:]=scipy.polyval(c_co[i,:],np.arange(len(Centers[i,:]))) + bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture+2) + sdthar = dthar.T - bac + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") + thar_S = np.zeros( (nord_co,3,dthar.shape[0]) ) + thar_Ss = np.zeros( (nord_co,dthar.shape[0]) ) + + tR,tG = fiesutils.get_RONGAIN(hthar[1].header) + #print tR,tG + thar_Ss = GLOBALutils.simple_extraction(sdthar,c_co,ext_aperture,\ + min_extract_col,max_extract_col,npools) + thar_Ss = thar_Ss[::-1] + thar_Ss = GLOBALutils.invert(thar_Ss) + if (os.access(thar_co_fits,os.F_OK)): + os.remove( thar_co_fits ) + if (os.access(thar_co_simple_fits,os.F_OK)): + os.remove( thar_co_simple_fits ) + + hdu = pyfits.PrimaryHDU( thar_S ) + hdu.writeto( thar_co_fits ) + hdu = pyfits.PrimaryHDU( thar_Ss ) + hdu.writeto( thar_co_simple_fits ) + else: + print("\t\tThAr file", fsim, "all ready extracted, loading...") for fsim in ThAr_sim: - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' - thar_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' - thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' - thar_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' - if ( os.access(thar_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): - hthar = pyfits.open( fsim ) - dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias - Centers = np.zeros((len(c_tot),dthar.shape[0])) - ccc = c_tot.copy() - - for i in range(Centers.shape[0]): - Centers[i,:]=scipy.polyval(ccc[i,:],np.arange(len(Centers[i,:]))) - - bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture,option=1) - sdthar = dthar.T - bac - - print "\t\tNo previous extraction or extraction forced for simultaneous ThAr file", fsim, "extracting..." - thar_S = np.zeros( (nord,3,dthar.shape[0]) ) - thar_Ss = np.zeros( (nord,dthar.shape[0]) ) - thar_S_co = np.zeros( (nord_co,3,dthar.shape[0]) ) - thar_S_co = np.zeros( (nord_co,3,dthar.shape[0]) ) - - tR,tG = fiesutils.get_RONGAIN(hthar[1].header) - #print tR,tG - thar_Ss = GLOBALutils.simple_extraction(sdthar,c_all,ext_aperture,\ - min_extract_col,max_extract_col,npools) - thar_Ss_co = GLOBALutils.simple_extraction(sdthar,c_co,ext_aperture,\ - min_extract_col,max_extract_col,npools) - thar_Ss = thar_Ss[::-1] - thar_Ss = GLOBALutils.invert(thar_Ss) - if (os.access(thar_fits,os.F_OK)): - os.remove( thar_fits ) - if (os.access(thar_simple_fits,os.F_OK)): - os.remove( thar_simple_fits ) - - thar_Ss_co = thar_Ss_co[::-1] - thar_Ss_co = GLOBALutils.invert(thar_Ss_co) - if (os.access(thar_co_fits,os.F_OK)): - os.remove( thar_co_fits ) - if (os.access(thar_co_simple_fits,os.F_OK)): - os.remove( thar_co_simple_fits ) - - hdu = pyfits.PrimaryHDU( thar_S ) - hdu.writeto( thar_fits ) - hdu = pyfits.PrimaryHDU( thar_Ss ) - hdu.writeto( thar_simple_fits ) - - hdu = pyfits.PrimaryHDU( thar_S_co ) - hdu.writeto( thar_co_fits ) - hdu = pyfits.PrimaryHDU( thar_Ss_co ) - hdu.writeto( thar_co_simple_fits ) - else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' + thar_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' + thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' + thar_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' + if ( os.access(thar_simple_fits,os.F_OK) == False ) or ( force_thar_extract ): + hthar = pyfits.open( fsim ) + dthar = fiesutils.OverscanTrim( hthar[1].data, binning=binning, ii=oii, ff=off ) - MasterBias + Centers = np.zeros((len(c_tot),dthar.shape[0])) + ccc = c_tot.copy() + + for i in range(Centers.shape[0]): + Centers[i,:]=scipy.polyval(ccc[i,:],np.arange(len(Centers[i,:]))) + + bac = GLOBALutils.get_scat(dthar.T,Centers,span=ext_aperture,option=1) + sdthar = dthar.T - bac + + print("\t\tNo previous extraction or extraction forced for simultaneous ThAr file", fsim, "extracting...") + thar_S = np.zeros( (nord,3,dthar.shape[0]) ) + thar_Ss = np.zeros( (nord,dthar.shape[0]) ) + thar_S_co = np.zeros( (nord_co,3,dthar.shape[0]) ) + thar_S_co = np.zeros( (nord_co,3,dthar.shape[0]) ) + + tR,tG = fiesutils.get_RONGAIN(hthar[1].header) + #print tR,tG + thar_Ss = GLOBALutils.simple_extraction(sdthar,c_all,ext_aperture,\ + min_extract_col,max_extract_col,npools) + thar_Ss_co = GLOBALutils.simple_extraction(sdthar,c_co,ext_aperture,\ + min_extract_col,max_extract_col,npools) + thar_Ss = thar_Ss[::-1] + thar_Ss = GLOBALutils.invert(thar_Ss) + if (os.access(thar_fits,os.F_OK)): + os.remove( thar_fits ) + if (os.access(thar_simple_fits,os.F_OK)): + os.remove( thar_simple_fits ) + + thar_Ss_co = thar_Ss_co[::-1] + thar_Ss_co = GLOBALutils.invert(thar_Ss_co) + if (os.access(thar_co_fits,os.F_OK)): + os.remove( thar_co_fits ) + if (os.access(thar_co_simple_fits,os.F_OK)): + os.remove( thar_co_simple_fits ) + + hdu = pyfits.PrimaryHDU( thar_S ) + hdu.writeto( thar_fits ) + hdu = pyfits.PrimaryHDU( thar_Ss ) + hdu.writeto( thar_simple_fits ) + + hdu = pyfits.PrimaryHDU( thar_S_co ) + hdu.writeto( thar_co_fits ) + hdu = pyfits.PrimaryHDU( thar_Ss_co ) + hdu.writeto( thar_co_simple_fits ) + else: + print("\t\tThAr file", fsim, "all ready extracted, loading...") """ p0 = np.zeros( npar_wsol ) -#p0[0] = (int(.5*n_useful)+ro0) * Global_ZP +#p0[0] = (int(.5*n_useful)+ro0) * Global_ZP dat = np.loadtxt('wavcals/initial.txt') p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(dat[:,2], dat[:,1],\ - dat[:,0], np.ones(len(dat[:,0])), p0, Cheby=use_cheby, \ - order0=79, ntotal=nord, maxrms=100000, Inv=Inverse_m, minlines=10, \ - npix=S_flat_n.shape[2],nx=3,nm=4) + dat[:,0], np.ones(len(dat[:,0])), p0, Cheby=use_cheby, \ + order0=79, ntotal=nord, maxrms=100000, Inv=Inverse_m, minlines=10, \ + npix=S_flat_n.shape[2],nx=3,nm=4) #f = open('thar_list.txt','r') f = open('lovis.txt','r') lines = f.readlines() wv,tp = [],[] for line in lines: - cos = line.split() - wv.append(float(cos[0])) - if len(cos)==4: - tp.append(cos[3]) - else: - tp.append(' ') + cos = line.split() + wv.append(float(cos[0])) + if len(cos)==4: + tp.append(cos[3]) + else: + tp.append(' ') wv,tp = np.array(wv),np.array(tp) sc =pyfits.getdata(dirout + 'FIyh230033.spec.simple.fits.S') for i in range(sc.shape[0]): - print i - sorder = str(i) - if i <10: - sorder = '0'+sorder - fout = open('wavcals/order_'+sorder+'.iwdat','w') - m = i + 79 - ejx = np.arange(sc.shape[1]) - ejy = sc[i] - chebs = GLOBALutils.Calculate_chebs(ejx, np.zeros(len(ejx))+i+79,Inverse=True,order0=79,ntotal=nord,npix=len(ejx),nx=ncoef_x,nm=ncoef_m) - wavsol = GLOBALutils.ToVacuum(1.0/float(m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m)) - print wavsol - tck = scipy.interpolate.splrep(wavsol,ejx,k=3) - I = np.where((wv>wavsol[50])&(wv0: - px = np.around(scipy.interpolate.splev(wv[I],tck)).astype('int') - tpx,twv = [],[] - for o in range(len(px)): - if ejy[px[o]]> 100: - if len(tlist) == 0: - wlist.append(wv[I][o]) - plist.append(px[o]) - tlist.append(tp[I][o]) - else: - if px[o] - plist[-1] < 8: - wlist.append(wv[I][o]) - plist.append(px[o]) - tlist.append(tp[I][o]) - else: - write = True - - if write: - lout = str(len(tlist)) - for ix in range(len(wlist)): - lout += '\t'+str(plist[ix])+'\t'+str(wlist[ix]) - for ix in range(len(wlist)): - lout += '\t'+str(tlist[ix]) - lout+='\n' - fout.write(lout) - wlist,plist,tlist = [],[],[] - write=False - - #plot(ejx,ejy) - #plot(tpx,ejy[tpx],'ro') - #show() - #print ghj - fout.close() + print i + sorder = str(i) + if i <10: + sorder = '0'+sorder + fout = open('wavcals/order_'+sorder+'.iwdat','w') + m = i + 79 + ejx = np.arange(sc.shape[1]) + ejy = sc[i] + chebs = GLOBALutils.Calculate_chebs(ejx, np.zeros(len(ejx))+i+79,Inverse=True,order0=79,ntotal=nord,npix=len(ejx),nx=ncoef_x,nm=ncoef_m) + wavsol = GLOBALutils.ToVacuum(1.0/float(m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m)) + print wavsol + tck = scipy.interpolate.splrep(wavsol,ejx,k=3) + I = np.where((wv>wavsol[50])&(wv0: + px = np.around(scipy.interpolate.splev(wv[I],tck)).astype('int') + tpx,twv = [],[] + for o in range(len(px)): + if ejy[px[o]]> 100: + if len(tlist) == 0: + wlist.append(wv[I][o]) + plist.append(px[o]) + tlist.append(tp[I][o]) + else: + if px[o] - plist[-1] < 8: + wlist.append(wv[I][o]) + plist.append(px[o]) + tlist.append(tp[I][o]) + else: + write = True + + if write: + lout = str(len(tlist)) + for ix in range(len(wlist)): + lout += '\t'+str(plist[ix])+'\t'+str(wlist[ix]) + for ix in range(len(wlist)): + lout += '\t'+str(tlist[ix]) + lout+='\n' + fout.write(lout) + wlist,plist,tlist = [],[],[] + write=False + + #plot(ejx,ejy) + #plot(tpx,ejy[tpx],'ro') + #show() + #print ghj + fout.close() plot(G_wav,G_res,'r.') show() print p1 """ -print "\n\tWavelength solution of ThAr calibration spectra of object fibre:" +print("\n\tWavelength solution of ThAr calibration spectra of object fibre:") for fsim in ThAr_ref: - wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' - h = pyfits.open(fsim) - hd = pyfits.getheader(fsim) - ron,gain = fiesutils.get_RONGAIN(h[1].header) - if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on simple ThAr file", fsim - hthar = pyfits.open( fsim ) - mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' - thar_S = pyfits.getdata( thar_fits ) - lines_thar = thar_S.copy() - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - All_residuals = np.array([]) - - orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning, delt_or=25,suf=sufix) - print 'orders_ofset:',orders_offset - print 'rough_shift:',rough_shift - - orderi = 0 - if orders_offset < 0: - orderi = - orders_offset - orderf = nord - 1 - if orderf + orders_offset >= n_useful: - orderf = n_useful - orders_offset - 1 - - for order in range(orderi, orderf+1): - order_s = str(order+orders_offset) - if (order + orders_offset < 10): - order_s = '0' + str(order+orders_offset) - f = open(order_dir+'order_'+order_s+sufix,'r') - llins = f.readlines() - if len(llins)>5: - thar_order_orig = lines_thar[order] - #IV = iv_thar_ob_R[order,:] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. - wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ - rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) - if (order == int(.5*n_useful)): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - All_residuals = np.append( All_residuals, residuals ) - p0 = np.zeros( npar_wsol ) - p0[0] = int(.5*n_useful) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ - All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ - order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ - npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - pdict = {'orders_offset':orders_offset,'rough_shift':rough_shift,'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ - 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths, \ - 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers, 'All_Sigmas':All_Sigmas} - - pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) - else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl - pdict = pickle.load(open(wavsol_pkl,'r')) - -print "\n\tWavelength solution of ThAr calibration spectra of comparison fibre:" + wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' + h = pyfits.open(fsim) + hd = pyfits.getheader(fsim) + ron,gain = fiesutils.get_RONGAIN(h[1].header) + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): + print("\t\tWorking on simple ThAr file", fsim) + hthar = pyfits.open( fsim ) + mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' + thar_S = pyfits.getdata( thar_fits ) + lines_thar = thar_S.copy() + + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + All_residuals = np.array([]) + + orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning, delt_or=25,suf=sufix) + print('orders_ofset:',orders_offset) + print('rough_shift:',rough_shift) + + orderi = 0 + if orders_offset < 0: + orderi = - orders_offset + orderf = nord - 1 + if orderf + orders_offset >= n_useful: + orderf = n_useful - orders_offset - 1 + + for order in range(orderi, orderf+1): + order_s = str(order+orders_offset) + if (order + orders_offset < 10): + order_s = '0' + str(order+orders_offset) + f = open(order_dir+'order_'+order_s+sufix,'r') + llins = f.readlines() + if len(llins)>5: + thar_order_orig = lines_thar[order] + #IV = iv_thar_ob_R[order,:] + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. + wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ + rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) + if (order == int(.5*n_useful)): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + All_residuals = np.append( All_residuals, residuals ) + p0 = np.zeros( npar_wsol ) + p0[0] = int(.5*n_useful) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ + All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ + order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ + npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + pdict = {'orders_offset':orders_offset,'rough_shift':rough_shift,'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ + 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths, \ + 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers, 'All_Sigmas':All_Sigmas} + + pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) + else: + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) + pdict = pickle.load(open(wavsol_pkl,'r')) + +print("\n\tWavelength solution of ThAr calibration spectra of comparison fibre:") for fsim in ThAr_co: - wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' - h = pyfits.open(fsim) - mjd, mjd0 = fiesutils.mjd_fromheader2( h ) - hd = pyfits.getheader(fsim) - ron,gain = fiesutils.get_RONGAIN(h[1].header) - if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on simple ThAr file", fsim - hthar = pyfits.open( fsim ) - - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' - thar_S = pyfits.getdata( thar_fits ) - lines_thar = thar_S.copy() - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - All_residuals = np.array([]) - - orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix,delt_or=25) - print 'orders_ofset:',orders_offset - print 'rough_shift:',rough_shift - - orderi = 0 - if orders_offset < 0: - orderi = - orders_offset - orderf = nord_co - 1 - if orderf + orders_offset >= n_useful: - orderf = n_useful - orders_offset - 1 - - for order in range(orderi,orderf+1): - order_s = str(order+orders_offset) - if (order + orders_offset < 10): - order_s = '0' + str(order+orders_offset) - f = open(order_dir+'order_'+order_s+sufix,'r') - llins = f.readlines() - if len(llins)>5: - thar_order_orig = lines_thar[order] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. - wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ - rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) - if (order == int(.5*n_useful)): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - All_residuals = np.append( All_residuals, residuals ) - p0 = np.zeros( npar_wsol ) - p0[0] = int(.5*n_useful) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ - All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ - order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ - npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - I = np.argmin((ThAr_ref_dates - mjd)**2) - pdict = {'orders_offset_co':orders_offset,'rough_shift_co':rough_shift,'p1_co':p1,'mjd_co':mjd,\ - 'G_pix_co':G_pix, 'G_ord_co':G_ord, 'G_wav_co':G_wav, 'II_co':II, 'rms_ms_co':rms_ms,\ - 'G_res_co':G_res, 'All_Centroids_co':All_Centroids, 'All_Wavelengths_co':All_Wavelengths,\ - 'All_Orders_co':All_Orders, 'All_Pixel_Centers_co':All_Pixel_Centers, 'All_Sigmas_co':All_Sigmas,\ - 'ref_thar_ob':ThAr_ref[I]} - - pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) - else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl - pdict = pickle.load(open(wavsol_pkl,'r')) + wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' + h = pyfits.open(fsim) + mjd, mjd0 = fiesutils.mjd_fromheader2( h ) + hd = pyfits.getheader(fsim) + ron,gain = fiesutils.get_RONGAIN(h[1].header) + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): + print("\t\tWorking on simple ThAr file", fsim) + hthar = pyfits.open( fsim ) + + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' + thar_S = pyfits.getdata( thar_fits ) + lines_thar = thar_S.copy() + + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + All_residuals = np.array([]) + + orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix,delt_or=25) + print('orders_ofset:',orders_offset) + print('rough_shift:',rough_shift) + + orderi = 0 + if orders_offset < 0: + orderi = - orders_offset + orderf = nord_co - 1 + if orderf + orders_offset >= n_useful: + orderf = n_useful - orders_offset - 1 + + for order in range(orderi,orderf+1): + order_s = str(order+orders_offset) + if (order + orders_offset < 10): + order_s = '0' + str(order+orders_offset) + f = open(order_dir+'order_'+order_s+sufix,'r') + llins = f.readlines() + if len(llins)>5: + thar_order_orig = lines_thar[order] + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. + wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ + rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) + if (order == int(.5*n_useful)): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + All_residuals = np.append( All_residuals, residuals ) + p0 = np.zeros( npar_wsol ) + p0[0] = int(.5*n_useful) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ + All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ + order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ + npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + I = np.argmin((ThAr_ref_dates - mjd)**2) + pdict = {'orders_offset_co':orders_offset,'rough_shift_co':rough_shift,'p1_co':p1,'mjd_co':mjd,\ + 'G_pix_co':G_pix, 'G_ord_co':G_ord, 'G_wav_co':G_wav, 'II_co':II, 'rms_ms_co':rms_ms,\ + 'G_res_co':G_res, 'All_Centroids_co':All_Centroids, 'All_Wavelengths_co':All_Wavelengths,\ + 'All_Orders_co':All_Orders, 'All_Pixel_Centers_co':All_Pixel_Centers, 'All_Sigmas_co':All_Sigmas,\ + 'ref_thar_ob':ThAr_ref[I]} + + pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) + else: + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) + pdict = pickle.load(open(wavsol_pkl,'r')) for fsim in ThAr_sim: - wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' - h = pyfits.open(fsim) - hd = pyfits.getheader(fsim) - ron,gain = fiesutils.get_RONGAIN(h[1].header) - if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on sim ThAr file", fsim - hthar = pyfits.open( fsim ) - mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' - thar_S = pyfits.getdata( thar_fits ) - lines_thar = thar_S.copy() - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - All_residuals = np.array([]) - xcs = [] - - orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix,delt_or=25) - print 'orders_ofset:',orders_offset - print 'rough_shift:',rough_shift - - orderi = 0 - if orders_offset < 0: - orderi = - orders_offset - orderf = nord - 1 - if orderf + orders_offset >= n_useful: - orderf = n_useful - orders_offset - 1 - - for order in range(orderi,orderf+1): - order_s = str(order+orders_offset) - if (order + orders_offset < 10): - order_s = '0' + str(order+orders_offset) - f = open(order_dir+'order_'+order_s+sufix,'r') - llins = f.readlines() - if len(llins)>5: - thar_order_orig = lines_thar[order] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. - wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ - rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) - if (order == int(.5*n_useful)): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - All_residuals = np.append( All_residuals, residuals ) - p0 = np.zeros( npar_wsol ) - p0[0] = int(.5*n_useful) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ - All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ - order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ - npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' - thar_S = pyfits.getdata( thar_co_fits ) - lines_thar = thar_S.copy() - - All_Pixel_Centers_co = np.array([]) - All_Wavelengths_co = np.array([]) - All_Orders_co = np.array([]) - All_Centroids_co = np.array([]) - All_Sigmas_co = np.array([]) - All_Intensities_co = np.array([]) - All_residuals_co = np.array([]) - - orders_offset_co, rough_shift_co = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix) - print 'orders_ofset_co:',orders_offset_co - print 'rough_shift_co:',rough_shift_co - - orderi = 0 - if orders_offset_co < 0: - orderi = - orders_offset_co - orderf = nord_co - 1 - if orderf + orders_offset_co >= n_useful: - orderf = n_useful - orders_offset_co - 1 - - for order in range(orderi,orderf+1): - #print order - order_s = str(order+orders_offset_co) - if (order + orders_offset_co < 10): - order_s = '0' + str(order+orders_offset_co) - f = open(order_dir+'order_'+order_s+sufix,'r') - llins = f.readlines() - if len(llins)>5: - thar_order_orig = lines_thar[order] - #IV = iv_thar_ob_R[order,:] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. - wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ - rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) - if (order == int(.5*n_useful)): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - - All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) - All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) - All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order) - All_Centroids_co = np.append( All_Centroids_co, centroids) - All_Sigmas_co = np.append( All_Sigmas_co, sigmas) - All_Intensities_co = np.append( All_Intensities_co, intensities ) - All_residuals_co = np.append( All_residuals_co, residuals ) - - p0 = np.zeros( npar_wsol ) - p0[0] = int(.5*n_useful) * Global_ZP - p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co,\ - All_Orders_co, np.ones(All_Intensities_co.shape), p0, Cheby=use_cheby,\ - order0=ro0+orders_offset_co, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m,\ - minlines=minlines_glob, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - pdict = {'orders_offset':orders_offset,'orders_offset_co':orders_offset_co, 'rough_shift':rough_shift,\ - 'rough_shift_co':rough_shift_co,'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ - 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths, \ - 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers, 'All_Sigmas':All_Sigmas,\ - 'p1_co':p1_co, 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, 'G_wav_co':G_wav_co, 'II_co':II_co,\ - 'rms_ms_co':rms_ms_co, 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths,\ - 'All_Orders_co':All_Orders_co, 'All_Pixel_Centers_co':All_Pixel_Centers_co, 'All_Sigmas_co':All_Sigmas_co} - pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) - - else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl - pdict = pickle.load(open(wavsol_pkl,'r')) + wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl' + h = pyfits.open(fsim) + hd = pyfits.getheader(fsim) + ron,gain = fiesutils.get_RONGAIN(h[1].header) + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): + print("\t\tWorking on sim ThAr file", fsim) + hthar = pyfits.open( fsim ) + mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' + thar_S = pyfits.getdata( thar_fits ) + lines_thar = thar_S.copy() + + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + All_residuals = np.array([]) + xcs = [] + + orders_offset, rough_shift = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix,delt_or=25) + print('orders_ofset:',orders_offset) + print('rough_shift:',rough_shift) + + orderi = 0 + if orders_offset < 0: + orderi = - orders_offset + orderf = nord - 1 + if orderf + orders_offset >= n_useful: + orderf = n_useful - orders_offset - 1 + + for order in range(orderi,orderf+1): + order_s = str(order+orders_offset) + if (order + orders_offset < 10): + order_s = '0' + str(order+orders_offset) + f = open(order_dir+'order_'+order_s+sufix,'r') + llins = f.readlines() + if len(llins)>5: + thar_order_orig = lines_thar[order] + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. + wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ + rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) + if (order == int(.5*n_useful)): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + All_residuals = np.append( All_residuals, residuals ) + p0 = np.zeros( npar_wsol ) + p0[0] = int(.5*n_useful) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths,\ + All_Orders, np.ones(All_Intensities.shape), p0, Cheby=use_cheby, \ + order0=ro0+orders_offset, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, \ + npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + thar_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' + thar_S = pyfits.getdata( thar_co_fits ) + lines_thar = thar_S.copy() + + All_Pixel_Centers_co = np.array([]) + All_Wavelengths_co = np.array([]) + All_Orders_co = np.array([]) + All_Centroids_co = np.array([]) + All_Sigmas_co = np.array([]) + All_Intensities_co = np.array([]) + All_residuals_co = np.array([]) + + orders_offset_co, rough_shift_co = fiesutils.get_thar_offsets(lines_thar,binning=binning,suf=sufix) + print('orders_ofset_co:',orders_offset_co) + print('rough_shift_co:',rough_shift_co) + + orderi = 0 + if orders_offset_co < 0: + orderi = - orders_offset_co + orderf = nord_co - 1 + if orderf + orders_offset_co >= n_useful: + orderf = n_useful - orders_offset_co - 1 + + for order in range(orderi,orderf+1): + #print order + order_s = str(order+orders_offset_co) + if (order + orders_offset_co < 10): + order_s = '0' + str(order+orders_offset_co) + f = open(order_dir+'order_'+order_s+sufix,'r') + llins = f.readlines() + if len(llins)>5: + thar_order_orig = lines_thar[order] + #IV = iv_thar_ob_R[order,:] + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. + wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ + rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) + if (order == int(.5*n_useful)): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) + All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) + All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order) + All_Centroids_co = np.append( All_Centroids_co, centroids) + All_Sigmas_co = np.append( All_Sigmas_co, sigmas) + All_Intensities_co = np.append( All_Intensities_co, intensities ) + All_residuals_co = np.append( All_residuals_co, residuals ) + + p0 = np.zeros( npar_wsol ) + p0[0] = int(.5*n_useful) * Global_ZP + p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co,\ + All_Orders_co, np.ones(All_Intensities_co.shape), p0, Cheby=use_cheby,\ + order0=ro0+orders_offset_co, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m,\ + minlines=minlines_glob, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + pdict = {'orders_offset':orders_offset,'orders_offset_co':orders_offset_co, 'rough_shift':rough_shift,\ + 'rough_shift_co':rough_shift_co,'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ + 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths, \ + 'All_Orders':All_Orders, 'All_Pixel_Centers':All_Pixel_Centers, 'All_Sigmas':All_Sigmas,\ + 'p1_co':p1_co, 'G_pix_co':G_pix_co, 'G_ord_co':G_ord_co, 'G_wav_co':G_wav_co, 'II_co':II_co,\ + 'rms_ms_co':rms_ms_co, 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Wavelengths':All_Wavelengths,\ + 'All_Orders_co':All_Orders_co, 'All_Pixel_Centers_co':All_Pixel_Centers_co, 'All_Sigmas_co':All_Sigmas_co} + pickle.dump( pdict, open(wavsol_pkl, 'w' ) ) + + else: + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) + pdict = pickle.load(open(wavsol_pkl,'r')) if len(ThAr_sim)>0: - wavsol_pkl = dirout + ThAr_sim[0].split('/')[-1][:-4]+'wavsolpars.pkl' - dct = pickle.load(open(wavsol_pkl,'r')) - p_ref = dct['p1'] - p_ref_co = dct['p1_co'] - orders_offset = dct['orders_offset'] - orders_offset_co = dct['orders_offset_co'] - rough_shift = dct['rough_shift'] - rough_shift_co = dct['rough_shift_co'] + wavsol_pkl = dirout + ThAr_sim[0].split('/')[-1][:-4]+'wavsolpars.pkl' + dct = pickle.load(open(wavsol_pkl,'r')) + p_ref = dct['p1'] + p_ref_co = dct['p1_co'] + orders_offset = dct['orders_offset'] + orders_offset_co = dct['orders_offset_co'] + rough_shift = dct['rough_shift'] + rough_shift_co = dct['rough_shift_co'] else: - if len(ThAr_ref)>0 and len(ThAr_co)>0: - wavsol_pkl = dirout + ThAr_co[0].split('/')[-1][:-4]+'wavsolpars.pkl' - dct = pickle.load(open(wavsol_pkl,'r')) - wavsol_ob_pkl = dirout + dct['ref_thar_ob'].split('/')[-1][:-4]+'wavsolpars.pkl' - dct_ob = pickle.load(open(wavsol_ob_pkl,'r')) - p_ref_co = dct['p1_co'] - p_ref = dct_ob['p1'] - orders_offset = dct_ob['orders_offset'] - orders_offset_co = dct['orders_offset_co'] - rough_shift = dct_ob['rough_shift'] - rough_shift_co = dct['rough_shift_co'] - - elif len(ThAr_ref)>0: - wavsol_pkl = dirout + ThAr_ref[0].split('/')[-1][:-4]+'wavsolpars.pkl' - dct = pickle.load(open(wavsol_pkl,'r')) - p_ref = dct['p1'] - orders_offset = dct['orders_offset'] - rough_shift = dct['rough_shift'] - + if len(ThAr_ref)>0 and len(ThAr_co)>0: + wavsol_pkl = dirout + ThAr_co[0].split('/')[-1][:-4]+'wavsolpars.pkl' + dct = pickle.load(open(wavsol_pkl,'r')) + wavsol_ob_pkl = dirout + dct['ref_thar_ob'].split('/')[-1][:-4]+'wavsolpars.pkl' + dct_ob = pickle.load(open(wavsol_ob_pkl,'r')) + p_ref_co = dct['p1_co'] + p_ref = dct_ob['p1'] + orders_offset = dct_ob['orders_offset'] + orders_offset_co = dct['orders_offset_co'] + rough_shift = dct_ob['rough_shift'] + rough_shift_co = dct['rough_shift_co'] + + elif len(ThAr_ref)>0: + wavsol_pkl = dirout + ThAr_ref[0].split('/')[-1][:-4]+'wavsolpars.pkl' + dct = pickle.load(open(wavsol_pkl,'r')) + p_ref = dct['p1'] + orders_offset = dct['orders_offset'] + rough_shift = dct['rough_shift'] + mjds_thar,shifts,shifts_co = [],[],[] -print '\n\tDetermination of instrumental drift through the night...' +print('\n\tDetermination of instrumental drift through the night...') for i in range(len(ThAr_sim)): - hthar = pyfits.open( ThAr_sim[i] ) - mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) - wavsol_pkl = dirout + ThAr_sim[i].split('/')[-1][:-4]+'wavsolpars.pkl' - dthar = pyfits.getdata( ThAr_sim[i] ) - npix = dthar.shape[0] - dct = pickle.load(open(wavsol_pkl,'r')) - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix'], dct['G_wav'], dct['G_ord'],\ - np.ones(len(dct['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - p_shift_co, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix_co'], dct['G_wav_co'], dct['G_ord_co'],\ - np.ones(len(dct['G_ord_co'])), p_ref_co, order0=ro0 + orders_offset_co, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - - if rms_ms / np.sqrt(float(len(orders))) < 10. and rms_ms_co / np.sqrt(float(len(orders_co))) < 10.: - shifts.append(p_shift[0]) - shifts_co.append(p_shift_co[0]) - mjds_thar.append(mjd) + hthar = pyfits.open( ThAr_sim[i] ) + mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) + wavsol_pkl = dirout + ThAr_sim[i].split('/')[-1][:-4]+'wavsolpars.pkl' + dthar = pyfits.getdata( ThAr_sim[i] ) + npix = dthar.shape[0] + dct = pickle.load(open(wavsol_pkl,'r')) + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix'], dct['G_wav'], dct['G_ord'],\ + np.ones(len(dct['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + p_shift_co, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix_co'], dct['G_wav_co'], dct['G_ord_co'],\ + np.ones(len(dct['G_ord_co'])), p_ref_co, order0=ro0 + orders_offset_co, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + + if rms_ms / np.sqrt(float(len(orders))) < 10. and rms_ms_co / np.sqrt(float(len(orders_co))) < 10.: + shifts.append(p_shift[0]) + shifts_co.append(p_shift_co[0]) + mjds_thar.append(mjd) used_thars = [] for i in range(len(ThAr_co)): - hthar = pyfits.open( ThAr_co[i] ) - mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) - wavsol_pkl = dirout + ThAr_co[i].split('/')[-1][:-4]+'wavsolpars.pkl' - dthar = pyfits.getdata( ThAr_co[i] ) - npix = dthar.shape[0] - dct = pickle.load(open(wavsol_pkl,'r')) - - wavsol_ob_pkl = dirout + dct['ref_thar_ob'].split('/')[-1][:-4]+'wavsolpars.pkl' - dct_ob = pickle.load(open(wavsol_ob_pkl,'r')) - used_thars.append(dct['ref_thar_ob']) - - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct_ob['G_pix'], dct_ob['G_wav'], dct_ob['G_ord'],\ - np.ones(len(dct_ob['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - p_shift_co, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix_co'], dct['G_wav_co'], dct['G_ord_co'],\ - np.ones(len(dct['G_ord_co'])), p_ref_co, order0=ro0 + orders_offset_co, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - - if rms_ms / np.sqrt(float(len(orders))) < 10. and rms_ms_co / np.sqrt(float(len(orders_co))) < 10.: - shifts.append(p_shift[0]) - shifts_co.append(p_shift_co[0]) - mjds_thar.append(mjd) + hthar = pyfits.open( ThAr_co[i] ) + mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) + wavsol_pkl = dirout + ThAr_co[i].split('/')[-1][:-4]+'wavsolpars.pkl' + dthar = pyfits.getdata( ThAr_co[i] ) + npix = dthar.shape[0] + dct = pickle.load(open(wavsol_pkl,'r')) + + wavsol_ob_pkl = dirout + dct['ref_thar_ob'].split('/')[-1][:-4]+'wavsolpars.pkl' + dct_ob = pickle.load(open(wavsol_ob_pkl,'r')) + used_thars.append(dct['ref_thar_ob']) + + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(dct_ob['G_pix'], dct_ob['G_wav'], dct_ob['G_ord'],\ + np.ones(len(dct_ob['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + p_shift_co, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix_co'], dct['G_wav_co'], dct['G_ord_co'],\ + np.ones(len(dct['G_ord_co'])), p_ref_co, order0=ro0 + orders_offset_co, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + + if rms_ms / np.sqrt(float(len(orders))) < 10. and rms_ms_co / np.sqrt(float(len(orders_co))) < 10.: + shifts.append(p_shift[0]) + shifts_co.append(p_shift_co[0]) + mjds_thar.append(mjd) used_thars = np.array(used_thars) for i in range(len(ThAr_ref_dates)): - if not ThAr_ref[i] in used_thars: - hthar = pyfits.open( ThAr_ref[i] ) - mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) - wavsol_pkl = dirout + ThAr_ref[i].split('/')[-1][:-4]+'wavsolpars.pkl' - dthar = pyfits.getdata( ThAr_ref[i] ) - npix = dthar.shape[0] - dct = pickle.load(open(wavsol_pkl,'r')) - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix'], dct['G_wav'], dct['G_ord'],\ - np.ones(len(dct['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - - if rms_ms / np.sqrt(float(len(orders))) < 10.: - shifts.append(p_shift[0]) - shifts_co.append(p_shift[0]) - mjds_thar.append(mjd) + if not ThAr_ref[i] in used_thars: + hthar = pyfits.open( ThAr_ref[i] ) + mjd, mjd0 = fiesutils.mjd_fromheader2( hthar ) + wavsol_pkl = dirout + ThAr_ref[i].split('/')[-1][:-4]+'wavsolpars.pkl' + dthar = pyfits.getdata( ThAr_ref[i] ) + npix = dthar.shape[0] + dct = pickle.load(open(wavsol_pkl,'r')) + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(dct['G_pix'], dct['G_wav'], dct['G_ord'],\ + np.ones(len(dct['G_ord'])), p_ref, order0=ro0 + orders_offset, npix=npix,\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + + if rms_ms / np.sqrt(float(len(orders))) < 10.: + shifts.append(p_shift[0]) + shifts_co.append(p_shift[0]) + mjds_thar.append(mjd) mjds_thar,shifts = np.array(mjds_thar),np.array(shifts) I = np.argsort(mjds_thar) @@ -987,8 +988,8 @@ shv = (1e-6*shifts)*299792458.0 if len(shifts)>1: - tck_v = scipy.interpolate.splrep(mjds_thar,shv,k=1) - tck_shift = scipy.interpolate.splrep(mjds_thar,shifts,k=1) + tck_v = scipy.interpolate.splrep(mjds_thar,shv,k=1) + tck_shift = scipy.interpolate.splrep(mjds_thar,shifts,k=1) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -1012,11 +1013,11 @@ new_list = [] new_times = [] for fsim in sim_sci: - h = pyfits.open(fsim) - if object2do in h[0].header['OBJECT'] or object2do == 'all': - new_list.append(fsim) - mjd, mjd0 = fiesutils.mjd_fromheader2( h ) - new_times.append(mjd) + h = pyfits.open(fsim) + if object2do in h[0].header['OBJECT'] or object2do == 'all': + new_list.append(fsim) + mjd, mjd0 = fiesutils.mjd_fromheader2( h ) + new_times.append(mjd) new_list = np.array(new_list) new_times = np.array(new_times) @@ -1026,587 +1027,587 @@ for fsim in new_list: - know_moon = False - if fsim.split('/')[-1] in spec_moon: - I = np.where(fsim.split('/')[-1] == spec_moon)[0] - know_moon = True - here_moon = use_moon[I] - - print '\n' - print "\t--> Working on image: ", fsim - h = pyfits.open(fsim) - mjd,mjd0 = fiesutils.mjd_fromheader2(h) - ronoise, gain = fiesutils.get_RONGAIN(h[1].header) - - # Object name - obname = h[0].header['OBJECT'] - print "\t\tObject name:",obname - - # Open file, trim, overscan subtract and MasterBias subtract - data = h[1].data - data = fiesutils.OverscanTrim( data, binning=binning, ii=oii, ff=off ) - MasterBias - if dark_corr and len(darks)>0 and int(h[0].header['EXPTIME']) in dark_utimes.astype('int'): - I = np.where(dark_utimes.astype('int') == int(h[0].header['EXPTIME']))[0] - data = data - pyfits.getdata(dark_names[I][0]) - - simult = False - if h[0].header['FILMP4'] == 1: - simult = True - - bacfile = dirout + 'BAC_' + fsim.split('/')[-1][:-4]+'fits' - if os.access(bacfile,os.F_OK)==False: - if simult: - Centers = np.zeros((len(c_tot),dthar.shape[0])) - ccc = c_tot.copy() - else: - Centers = np.zeros((len(c_all),dthar.shape[0])) - ccc = c_all.copy() - - for i in range(Centers.shape[0]): - Centers[i,:]=scipy.polyval(ccc[i,:],np.arange(len(Centers[i,:]))) - - if simult: - bac = GLOBALutils.get_scat(data.T,Centers,span=ext_aperture,option=1) - else: - bac = GLOBALutils.get_scat(data.T,Centers,span=bacap) - - if (os.access(bacfile,os.F_OK)): - os.remove( bacfile ) - hdbac = pyfits.PrimaryHDU( bac ) - hdbac.writeto(bacfile) - else: - bac = pyfits.getdata(bacfile) - data = data.T - bac - - ra = float(h[0].header['RA']) - dec = float(h[0].header['DEC']) - altitude = 2382. - latitude = 28.75722 - longitude = -17.885 - epoch = 2000. - - ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) - if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 - else: - print '\t\tUsing the coordinates found in the image header.' - - iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) - obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) - obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) - jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) - jplephem.set_observer_coordinates( float(obpos[0]), float(obpos[1]), float(obpos[2]) ) - res = jplephem.doppler_fraction(float(ra/15.0), float(dec), long(mjd), float(mjd%1), 1, 0.0) - lbary_ltopo = 1.0 + res['frac'][0] - bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc - res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) - mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) - - gobs = ephem.Observer() - gobs.name = 'La Palma' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) - gobs.date = h[0].header['DATE-OBS'][:10] + ' ' + h[0].header['DATE-OBS'][11:] - mephem = ephem.Moon() - mephem.compute(gobs) - - Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) - res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) - lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) - refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel - - print '\t\tExtraction:' - sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' - sci_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' - if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or \ - ( force_sci_extract ): - - print "\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." - sci_Ss = GLOBALutils.simple_extraction(data,c_all,ext_aperture,\ - min_extract_col,max_extract_col,npools) - sci_S = GLOBALutils.optimal_extraction(data,P,c_all,ext_aperture,\ - ronoise,gain,S_Marsh,NCosmic_Marsh,\ - min_extract_col,max_extract_col,npools) - - sci_Ss = sci_Ss[::-1] - sci_Ss = GLOBALutils.invert(sci_Ss) - - for iii in range(3): - sci_St = sci_S[:,iii].copy() - sci_St = sci_St[::-1] - sci_S[:,iii] = sci_St - sci_S = GLOBALutils.invert(sci_S) - - if (os.access(sci_fits,os.F_OK)): - os.remove( sci_fits ) - if (os.access(sci_fits_simple,os.F_OK)): - os.remove( sci_fits_simple ) - - hdu = pyfits.PrimaryHDU( sci_S ) - hdu.writeto( sci_fits ) - hdu = pyfits.PrimaryHDU( sci_Ss ) - hdu.writeto( sci_fits_simple ) - - - else: - print '\t\t\t '+fsim+" has already been extracted, reading in product fits files..." - sci_S = pyfits.getdata( sci_fits ) - sci_Ss = pyfits.getdata( sci_fits_simple ) - - if simult: - sci_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' - sci_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' - if ( os.access(sci_co_fits,os.F_OK) == False ) or ( os.access(sci_co_simple_fits,os.F_OK) == False ) or \ - ( force_sci_extract ): - - print "\t\t\tNo previous extraction or extraction forced for comparison orders of science file", fsim, "extracting..." - sci_co_Ss = GLOBALutils.simple_extraction(data,c_co,ext_aperture,\ - min_extract_col,max_extract_col,npools) - sci_co_S = GLOBALutils.optimal_extraction(data,P_co,c_co,ext_aperture,\ - ronoise,gain,S_Marsh,NCosmic_Marsh,\ - min_extract_col,max_extract_col,npools) - - sci_co_Ss = sci_co_Ss[::-1] - sci_co_Ss = GLOBALutils.invert(sci_co_Ss) - - for iii in range(3): - sci_co_St = sci_co_S[:,iii].copy() - sci_co_St = sci_co_St[::-1] - sci_co_S[:,iii] = sci_co_St - sci_co_S = GLOBALutils.invert(sci_co_S) - - if (os.access(sci_co_fits,os.F_OK)): - os.remove( sci_co_fits ) - if (os.access(sci_co_simple_fits,os.F_OK)): - os.remove( sci_co_simple_fits ) - - hdu = pyfits.PrimaryHDU( sci_co_S ) - hdu.writeto( sci_co_fits ) - hdu = pyfits.PrimaryHDU( sci_co_Ss ) - hdu.writeto( sci_co_simple_fits ) - - - else: - print '\t\t\t '+fsim+" has already been extracted, reading in product fits files..." - sci_co_S = pyfits.getdata( sci_co_fits ) - sci_co_Ss = pyfits.getdata( sci_co_simple_fits ) - - - fout = 'proc/' + obname + '_' + h[0].header['DATE-OBS'] + '_sp.fits' - if ( os.access(dirout+fout ,os.F_OK) == False ) or (force_spectral_file_build): - orderi = 0 - if orders_offset < 0: - orderi = - orders_offset - orderf = nord - 1 - if orderf + orders_offset >= n_useful: - orderf = n_useful - orders_offset - 1 - - spec = np.zeros((11, orderf - orderi + 1, data.shape[1])) - hdu = pyfits.PrimaryHDU( spec ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', h[0].header['DATE-OBS'][:10] ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'][11:]) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) - hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000.) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS START',h[0].header['AIRMASS']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON VEL',refvel) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMULT',simult) - - print '\t\tWavelength calibration:' - - if simult and (len(ThAr_co)>0 or len(ThAr_sim)>0): - lines_thar = sci_co_S[:,1,:].copy() - - All_Pixel_Centers_co = np.array([]) - All_Wavelengths_co = np.array([]) - All_Orders_co = np.array([]) - All_Centroids_co = np.array([]) - All_Sigmas_co = np.array([]) - All_Intensities_co = np.array([]) - All_residuals_co = np.array([]) - - orderi = 0 - if orders_offset_co < 0: - orderi = - orders_offset_co - orderf = nord_co - 1 - if orderf + orders_offset_co >= n_useful: - orderf = n_useful - orders_offset_co - 1 - - for order in range(orderi,orderf+1): - order_s = str(order+orders_offset_co) - if (order + orders_offset_co < 10): - order_s = '0' + str(order+orders_offset_co) - f = open(order_dir+'order_'+order_s+sufix,'r') - llins = f.readlines() - if len(llins)>5: - thar_order_orig = lines_thar[order] - L = np.where(thar_order_orig != 0)[0] - IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) - IV[L] = 0. - wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) - bkg = scipy.signal.medfilt(thar_order_orig,101) - thar_order = thar_order_orig - bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ - rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) - if (order == int(.5*n_useful)): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - - All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) - All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) - All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order) - All_Centroids_co = np.append( All_Centroids_co, centroids) - All_Sigmas_co = np.append( All_Sigmas_co, sigmas) - All_Intensities_co = np.append( All_Intensities_co, intensities ) - All_residuals_co = np.append( All_residuals_co, residuals ) - - p0 = np.zeros( npar_wsol ) - p0[0] = int(.5*n_useful) * Global_ZP - p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co,\ - All_Orders_co, np.ones(All_Intensities_co.shape), p0, Cheby=use_cheby,\ - order0=ro0+orders_offset_co, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m,\ - minlines=minlines_glob, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - p_shift, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co, G_wav_co, G_ord_co,\ - np.ones(len(G_ord_co)), p_ref_co, order0=ro0 + orders_offset_co, npix=len(thar_order),\ - Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) - - else: - if len(shifts)>1: - p_shift = scipy.interpolate.splev(mjd,tck_shift) - else: - p_shift = 0. - - orderi = 0 - if orders_offset < 0: - orderi = - orders_offset - orderf = nord - 1 - if orderf + orders_offset >= n_useful: - orderf = n_useful - orders_offset - 1 - - print "\t\t\tInstrumental drift:",(1e-6*p_shift)*299792458.0, 'm/s' - # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) - order = orderi - torder = 0 - while order < orderf+1: - m = order + ro0 + orders_offset - chebs = GLOBALutils.Calculate_chebs(equis, m, npix=sci_S.shape[2], order0=ro0 + orders_offset, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/float(m)) * \ - GLOBALutils.Joint_Polynomial_Cheby(p_ref,chebs,ncoef_x,ncoef_m) - - spec[0,torder,:] = WavSol - spec[1,torder,:] = sci_S[order,1] - spec[2,torder,:] = sci_S[order,2] - fn = S_flat[order,1,:] - L = np.where( fn == 0 )[0] - spec[3,torder,:] = spec[1,torder,:] / S_flat[order,1,:] - spec[4,torder,:] = spec[2,torder] * ( S_flat_n[order,1,:] ** 2 ) - spec[3,torder,L] = 0. - spec[4,torder,L] = 0. - ccoef = GLOBALutils.get_cont_single(spec[0,torder],spec[3,torder],spec[4,torder],ll=1.5,lu=5,nc=3) - L = np.where( spec[1,torder] != 0 ) - spec[5,torder,:][L] = spec[3,torder][L] / np.polyval(ccoef,spec[0,torder][L]) - ratio = np.polyval(ccoef,spec[0,torder][L]) * Snorms[order] - spec[6,torder,:][L] = spec[4,torder][L] * (ratio ** 2 ) - spec[7,torder,:][L] = ratio - spec[8,torder,:][L] = ratio * S_flat_n[order,1][L] / np.sqrt( ratio * S_flat_n[order,1][L] / gain + (ronoise/gain)**2 ) - spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) - dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - LL = np.where(spec[5,torder] > 1 + 10. / scipy.signal.medfilt(spec[8,torder],21))[0] - spec[5,torder,LL] = 1. - spec[9,torder][L] = spec[5,torder][L] * (dlambda_dx[L] ** 1) - spec[10,torder][L] = spec[6,torder][L] / (dlambda_dx[L] ** 2) - order +=1 - torder += 1 - #show() - if os.access(dirout + fout, os.F_OK): - os.remove(dirout + fout) - hdu.writeto(dirout + fout) - - if (not JustExtract): - - if DoClass: - print '\t\tSpectral Analysis:' - # spectral analysis - # First, query SIMBAD with the object name - query_success = False - sp_type_query = 'None' - #query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) - # Now, query SIMBAD by coordinates if above not successful - #if (not query_success): - # query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query - - hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) - - pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' - - if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" - spec2 = spec.copy() - if resol > 44000: - Rx = np.around(1./np.sqrt(1./40000.**2 - 1./resol**2)) - for i in range(spec.shape[1]): - IJ = np.where(spec[5,i]!=0.)[0] - spec2[5,i,IJ] = GLOBALutils.convolve(spec[0,i,IJ],spec[5,i,IJ],Rx) - T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec2,model_path=models_path,npools=npools) - line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) - f = open(pars_file,'w') - f.write(line) - f.close() - - else: - print "\t\t\tAtmospheric parameters loaded from file:" - T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 - - else: - T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 - - T_eff_epoch = T_eff - logg_epoch = logg - Z_epoch = Z - vsini_epoch = vsini - vel0_epoch = vel0 - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - - print "\t\tRadial Velocity analysis:" - # assign mask - sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - - # Read in mask - ml, mh, weight = np.loadtxt(mask,unpack=True) - ml_v = GLOBALutils.ToVacuum( ml ) - mh_v = GLOBALutils.ToVacuum( mh ) - - # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS - av_m = 0.5*( ml_v + mh_v ) - ml_v -= (av_m - ml_v) - mh_v += (mh_v - av_m) - mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m - - #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' - - disp = GLOBALutils.get_disp(obname, reffile=reffile) - if disp == 0: - known_sigma = False - if vsini != -999 and vsini != 0.: - disp = vsini - else: - disp = 3. - else: - known_sigma = True - - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' - cond = True - - while (cond): - # first rough correlation to find the minimum - vels, xc_full, sn, nlines_ccf, W_ccf = \ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=300,vel_step=3,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - - # Normalize the continuum of the CCF robustly with lowess - yy = scipy.signal.medfilt(xc_av,11) - pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) - tck1 = scipy.interpolate.splrep(vels,pred,k=1) - xc_av_orig = xc_av.copy() - xc_av /= pred - vel0_xc = vels[ np.argmin( xc_av ) ] - rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ - xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc - - xc_av_rough = xc_av - vels_rough = vels - if disp > 30: - disp = 30. - vel_width = np.maximum( 20.0, 6*disp ) - - vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.1,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - pred = scipy.interpolate.splev(vels,tck1) - xc_av /= pred - - if sp_type == 'M5': - moon_sig = 2.5 - elif sp_type == 'K5': - moon_sig = 3.3 - else: - moon_sig = 4.5 - - p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ - sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) - - moonmatters = False - if (know_moon and here_moon): - moonmatters = True - ismoon = True - confused = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) - moon_flag = 1 - else: - confused = False - ismoon = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 - moon_flag = 0 - - bspan = GLOBALutils.calc_bss(vels,xc_av) - SP = bspan[0] - - if (not known_sigma): - disp = np.floor(p1gau[2]) - if (disp < 3.0): - disp = 3.0 - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - known_sigma = True - else: - cond = False - - xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} - - moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':float(h[0].header['EXPTIME'])} - - pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' - pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - - ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' - - if not avoid_plot: - GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - - SNR_5130 = np.median(spec[8,32,900:1101] ) - airmass = float(h[0].header['AIRMASS']) - seeing = -999 - TEXP = float(h[0].header['EXPTIME']) - - if sp_type == 'G2': - if T_eff < 6000: - A = 0.06544 - B = 0.00146 - D = 0.24416 - C = 0.00181 - else: - A = 0.09821 - B = 0.00014 - D = 0.33491 - C = 0.00113 - elif sp_type == 'K5': - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 - else: - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 - - RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) - depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) - - if depth_fact >= 1.: - RVerr2 = -999.000 - else: - if sp_type == 'G2': - depth_fact = (1 - 0.62) / (1 - depth_fact) - else: - depth_fact = (1 - 0.59) / (1 - depth_fact) - RVerr2 = RVerr * depth_fact - if (RVerr2 <= 0.009): - RVerr2 = 0.009 - - BSerr = D / float(np.round(SNR_5130)) + C - - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) - RVerr2 = np.around(RVerr2,4) - BSerr = np.around(BSerr,4) - - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) - - bjd_out = 2400000.5 + mbjd - T_eff_err = 100 - logg_err = 0.5 - Z_err = 0.5 - vsini_err = 2 - XC_min = np.abs(np.around(np.min(XCmodel),2)) - - SNR_5130 = np.around(SNR_5130) - SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) - - disp_epoch = np.around(p1gau_m[2],1) - hdu = GLOBALutils.update_header(hdu,'RV', RV) - hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) - hdu = GLOBALutils.update_header(hdu,'BS', BS) - hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) - hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) - hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) - hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) - hdu = GLOBALutils.update_header(hdu,'INST', 'FIES') - hdu = GLOBALutils.update_header(hdu,'RESOL', str(resol)) - hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') - hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) - hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - - # write to output - line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f fies ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ - (obname, bjd_out, RV, RVerr2, BS, BSerr, resol, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) - f_res.write(line_out) - - if (os.access( dirout + fout,os.F_OK)): - os.remove( dirout + fout) - - hdu.writeto( dirout + fout ) - else: - print "Reading spectral file from", fout - spec = pyfits.getdata( fout ) - -f_res.close() \ No newline at end of file + know_moon = False + if fsim.split('/')[-1] in spec_moon: + I = np.where(fsim.split('/')[-1] == spec_moon)[0] + know_moon = True + here_moon = use_moon[I] + + print('\n') + print("\t--> Working on image: ", fsim) + h = pyfits.open(fsim) + mjd,mjd0 = fiesutils.mjd_fromheader2(h) + ronoise, gain = fiesutils.get_RONGAIN(h[1].header) + + # Object name + obname = h[0].header['OBJECT'] + print("\t\tObject name:",obname) + + # Open file, trim, overscan subtract and MasterBias subtract + data = h[1].data + data = fiesutils.OverscanTrim( data, binning=binning, ii=oii, ff=off ) - MasterBias + if dark_corr and len(darks)>0 and int(h[0].header['EXPTIME']) in dark_utimes.astype('int'): + I = np.where(dark_utimes.astype('int') == int(h[0].header['EXPTIME']))[0] + data = data - pyfits.getdata(dark_names[I][0]) + + simult = False + if h[0].header['FILMP4'] == 1: + simult = True + + bacfile = dirout + 'BAC_' + fsim.split('/')[-1][:-4]+'fits' + if os.access(bacfile,os.F_OK)==False: + if simult: + Centers = np.zeros((len(c_tot),dthar.shape[0])) + ccc = c_tot.copy() + else: + Centers = np.zeros((len(c_all),dthar.shape[0])) + ccc = c_all.copy() + + for i in range(Centers.shape[0]): + Centers[i,:]=scipy.polyval(ccc[i,:],np.arange(len(Centers[i,:]))) + + if simult: + bac = GLOBALutils.get_scat(data.T,Centers,span=ext_aperture,option=1) + else: + bac = GLOBALutils.get_scat(data.T,Centers,span=bacap) + + if (os.access(bacfile,os.F_OK)): + os.remove( bacfile ) + hdbac = pyfits.PrimaryHDU( bac ) + hdbac.writeto(bacfile) + else: + bac = pyfits.getdata(bacfile) + data = data.T - bac + + ra = float(h[0].header['RA']) + dec = float(h[0].header['DEC']) + altitude = 2382. + latitude = 28.75722 + longitude = -17.885 + epoch = 2000. + + ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) + if ra2 !=0 and dec2 != 0: + ra = ra2 + dec = dec2 + else: + print('\t\tUsing the coordinates found in the image header.') + + iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) + obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) + obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) + jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) + jplephem.set_observer_coordinates( float(obpos[0]), float(obpos[1]), float(obpos[2]) ) + res = jplephem.doppler_fraction(float(ra/15.0), float(dec), long(mjd), float(mjd%1), 1, 0.0) + lbary_ltopo = 1.0 + res['frac'][0] + bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 + print("\t\tBarycentric velocity:", bcvel_baryc) + res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) + + gobs = ephem.Observer() + gobs.name = 'La Palma' + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) + gobs.date = h[0].header['DATE-OBS'][:10] + ' ' + h[0].header['DATE-OBS'][11:] + mephem = ephem.Moon() + mephem.compute(gobs) + + Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) + res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) + lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) + refvel = bcvel_baryc + moonvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) + + print('\t\tExtraction:') + sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec.fits.S' + sci_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.fits.S' + if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or \ + ( force_sci_extract ): + + print("\t\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") + sci_Ss = GLOBALutils.simple_extraction(data,c_all,ext_aperture,\ + min_extract_col,max_extract_col,npools) + sci_S = GLOBALutils.optimal_extraction(data,P,c_all,ext_aperture,\ + ronoise,gain,S_Marsh,NCosmic_Marsh,\ + min_extract_col,max_extract_col,npools) + + sci_Ss = sci_Ss[::-1] + sci_Ss = GLOBALutils.invert(sci_Ss) + + for iii in range(3): + sci_St = sci_S[:,iii].copy() + sci_St = sci_St[::-1] + sci_S[:,iii] = sci_St + sci_S = GLOBALutils.invert(sci_S) + + if (os.access(sci_fits,os.F_OK)): + os.remove( sci_fits ) + if (os.access(sci_fits_simple,os.F_OK)): + os.remove( sci_fits_simple ) + + hdu = pyfits.PrimaryHDU( sci_S ) + hdu.writeto( sci_fits ) + hdu = pyfits.PrimaryHDU( sci_Ss ) + hdu.writeto( sci_fits_simple ) + + + else: + print('\t\t\t '+fsim+" has already been extracted, reading in product fits files...") + sci_S = pyfits.getdata( sci_fits ) + sci_Ss = pyfits.getdata( sci_fits_simple ) + + if simult: + sci_co_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.fits.S' + sci_co_simple_fits = dirout + fsim.split('/')[-1][:-4]+'spec.co.simple.fits.S' + if ( os.access(sci_co_fits,os.F_OK) == False ) or ( os.access(sci_co_simple_fits,os.F_OK) == False ) or \ + ( force_sci_extract ): + + print("\t\t\tNo previous extraction or extraction forced for comparison orders of science file", fsim, "extracting...") + sci_co_Ss = GLOBALutils.simple_extraction(data,c_co,ext_aperture,\ + min_extract_col,max_extract_col,npools) + sci_co_S = GLOBALutils.optimal_extraction(data,P_co,c_co,ext_aperture,\ + ronoise,gain,S_Marsh,NCosmic_Marsh,\ + min_extract_col,max_extract_col,npools) + + sci_co_Ss = sci_co_Ss[::-1] + sci_co_Ss = GLOBALutils.invert(sci_co_Ss) + + for iii in range(3): + sci_co_St = sci_co_S[:,iii].copy() + sci_co_St = sci_co_St[::-1] + sci_co_S[:,iii] = sci_co_St + sci_co_S = GLOBALutils.invert(sci_co_S) + + if (os.access(sci_co_fits,os.F_OK)): + os.remove( sci_co_fits ) + if (os.access(sci_co_simple_fits,os.F_OK)): + os.remove( sci_co_simple_fits ) + + hdu = pyfits.PrimaryHDU( sci_co_S ) + hdu.writeto( sci_co_fits ) + hdu = pyfits.PrimaryHDU( sci_co_Ss ) + hdu.writeto( sci_co_simple_fits ) + + + else: + print('\t\t\t '+fsim+" has already been extracted, reading in product fits files...") + sci_co_S = pyfits.getdata( sci_co_fits ) + sci_co_Ss = pyfits.getdata( sci_co_simple_fits ) + + + fout = 'proc/' + obname + '_' + h[0].header['DATE-OBS'] + '_sp.fits' + if ( os.access(dirout+fout ,os.F_OK) == False ) or (force_spectral_file_build): + orderi = 0 + if orders_offset < 0: + orderi = - orders_offset + orderf = nord - 1 + if orderf + orders_offset >= n_useful: + orderf = n_useful - orders_offset - 1 + + spec = np.zeros((11, orderf - orderi + 1, data.shape[1])) + hdu = pyfits.PrimaryHDU( spec ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', h[0].header['DATE-OBS'][:10] ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'][11:]) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) + hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000.) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS START',h[0].header['AIRMASS']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MOON VEL',refvel) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMULT',simult) + + print('\t\tWavelength calibration:') + + if simult and (len(ThAr_co)>0 or len(ThAr_sim)>0): + lines_thar = sci_co_S[:,1,:].copy() + + All_Pixel_Centers_co = np.array([]) + All_Wavelengths_co = np.array([]) + All_Orders_co = np.array([]) + All_Centroids_co = np.array([]) + All_Sigmas_co = np.array([]) + All_Intensities_co = np.array([]) + All_residuals_co = np.array([]) + + orderi = 0 + if orders_offset_co < 0: + orderi = - orders_offset_co + orderf = nord_co - 1 + if orderf + orders_offset_co >= n_useful: + orderf = n_useful - orders_offset_co - 1 + + for order in range(orderi,orderf+1): + order_s = str(order+orders_offset_co) + if (order + orders_offset_co < 10): + order_s = '0' + str(order+orders_offset_co) + f = open(order_dir+'order_'+order_s+sufix,'r') + llins = f.readlines() + if len(llins)>5: + thar_order_orig = lines_thar[order] + L = np.where(thar_order_orig != 0)[0] + IV = 1. / (thar_order_orig / gain + (ron/gain)**2 ) + IV[L] = 0. + wei = np.ones(len(thar_order_orig)) #np.sqrt( IV ) + bkg = scipy.signal.medfilt(thar_order_orig,101) + thar_order = thar_order_orig - bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids,sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'order_'+order_s+sufix, thar_order, order, wei, \ + rmsmax=500, minlines=10, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, line_width=6, Cheby=use_cheby,porder=3,rough_shift=rough_shift,binning=binning,del_width=5.,do_xc=False) + if (order == int(.5*n_useful)): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + + All_Pixel_Centers_co = np.append( All_Pixel_Centers_co, pixel_centers ) + All_Wavelengths_co = np.append( All_Wavelengths_co, wavelengths ) + All_Orders_co = np.append( All_Orders_co, np.zeros( len(pixel_centers) ) + order) + All_Centroids_co = np.append( All_Centroids_co, centroids) + All_Sigmas_co = np.append( All_Sigmas_co, sigmas) + All_Intensities_co = np.append( All_Intensities_co, intensities ) + All_residuals_co = np.append( All_residuals_co, residuals ) + + p0 = np.zeros( npar_wsol ) + p0[0] = int(.5*n_useful) * Global_ZP + p1_co, G_pix_co, G_ord_co, G_wav_co, II_co, rms_ms_co, G_res_co = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co, All_Wavelengths_co,\ + All_Orders_co, np.ones(All_Intensities_co.shape), p0, Cheby=use_cheby,\ + order0=ro0+orders_offset_co, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m,\ + minlines=minlines_glob, npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + p_shift, pix_centers_co, orders_co, wavelengths_co, I_co, rms_ms_co, residuals_co = \ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co, G_wav_co, G_ord_co,\ + np.ones(len(G_ord_co)), p_ref_co, order0=ro0 + orders_offset_co, npix=len(thar_order),\ + Cheby=use_cheby, ntotal=n_useful, maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob, nx=ncoef_x,nm=ncoef_m) + + else: + if len(shifts)>1: + p_shift = scipy.interpolate.splev(mjd,tck_shift) + else: + p_shift = 0. + + orderi = 0 + if orders_offset < 0: + orderi = - orders_offset + orderf = nord - 1 + if orderf + orders_offset >= n_useful: + orderf = n_useful - orders_offset - 1 + + print("\t\t\tInstrumental drift:",(1e-6*p_shift)*299792458.0, 'm/s') + # Apply new wavelength solution including barycentric correction + equis = np.arange( data.shape[1] ) + order = orderi + torder = 0 + while order < orderf+1: + m = order + ro0 + orders_offset + chebs = GLOBALutils.Calculate_chebs(equis, m, npix=sci_S.shape[2], order0=ro0 + orders_offset, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo * (1.0 + 1.0e-6*p_shift) * (1.0/float(m)) * \ + GLOBALutils.Joint_Polynomial_Cheby(p_ref,chebs,ncoef_x,ncoef_m) + + spec[0,torder,:] = WavSol + spec[1,torder,:] = sci_S[order,1] + spec[2,torder,:] = sci_S[order,2] + fn = S_flat[order,1,:] + L = np.where( fn == 0 )[0] + spec[3,torder,:] = spec[1,torder,:] / S_flat[order,1,:] + spec[4,torder,:] = spec[2,torder] * ( S_flat_n[order,1,:] ** 2 ) + spec[3,torder,L] = 0. + spec[4,torder,L] = 0. + ccoef = GLOBALutils.get_cont_single(spec[0,torder],spec[3,torder],spec[4,torder],ll=1.5,lu=5,nc=3) + L = np.where( spec[1,torder] != 0 ) + spec[5,torder,:][L] = spec[3,torder][L] / np.polyval(ccoef,spec[0,torder][L]) + ratio = np.polyval(ccoef,spec[0,torder][L]) * Snorms[order] + spec[6,torder,:][L] = spec[4,torder][L] * (ratio ** 2 ) + spec[7,torder,:][L] = ratio + spec[8,torder,:][L] = ratio * S_flat_n[order,1][L] / np.sqrt( ratio * S_flat_n[order,1][L] / gain + (ronoise/gain)**2 ) + spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) + dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + LL = np.where(spec[5,torder] > 1 + 10. / scipy.signal.medfilt(spec[8,torder],21))[0] + spec[5,torder,LL] = 1. + spec[9,torder][L] = spec[5,torder][L] * (dlambda_dx[L] ** 1) + spec[10,torder][L] = spec[6,torder][L] / (dlambda_dx[L] ** 2) + order +=1 + torder += 1 + #show() + if os.access(dirout + fout, os.F_OK): + os.remove(dirout + fout) + hdu.writeto(dirout + fout) + + if (not JustExtract): + + if DoClass: + print('\t\tSpectral Analysis:') + # spectral analysis + # First, query SIMBAD with the object name + query_success = False + sp_type_query = 'None' + #query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) + # Now, query SIMBAD by coordinates if above not successful + #if (not query_success): + # query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) + + hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) + + pars_file = dirout + fsim.split('/')[-1][:-8]+'_stellar_pars.txt' + + if os.access(pars_file,os.F_OK) == False or force_stellar_pars: + print("\t\t\tEstimating atmospheric parameters:") + spec2 = spec.copy() + if resol > 44000: + Rx = np.around(1./np.sqrt(1./40000.**2 - 1./resol**2)) + for i in range(spec.shape[1]): + IJ = np.where(spec[5,i]!=0.)[0] + spec2[5,i,IJ] = GLOBALutils.convolve(spec[0,i,IJ],spec[5,i,IJ],Rx) + T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec2,model_path=models_path,npools=npools) + line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) + f = open(pars_file,'w') + f.write(line) + f.close() + + else: + print("\t\t\tAtmospheric parameters loaded from file:") + T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) + + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) + + else: + T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + + T_eff_epoch = T_eff + logg_epoch = logg + Z_epoch = Z + vsini_epoch = vsini + vel0_epoch = vel0 + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) + + print("\t\tRadial Velocity analysis:") + # assign mask + sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') + print("\t\t\tWill use",sp_type,"mask for CCF.") + + # Read in mask + ml, mh, weight = np.loadtxt(mask,unpack=True) + ml_v = GLOBALutils.ToVacuum( ml ) + mh_v = GLOBALutils.ToVacuum( mh ) + + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS + av_m = 0.5*( ml_v + mh_v ) + ml_v -= (av_m - ml_v) + mh_v += (mh_v - av_m) + mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m + + #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' + + disp = GLOBALutils.get_disp(obname, reffile=reffile) + if disp == 0: + known_sigma = False + if vsini != -999 and vsini != 0.: + disp = vsini + else: + disp = 3. + else: + known_sigma = True + + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + + print('\t\t\tComputing the CCF...') + cond = True + + while (cond): + # first rough correlation to find the minimum + vels, xc_full, sn, nlines_ccf, W_ccf = \ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=300,vel_step=3,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) + + # Normalize the continuum of the CCF robustly with lowess + yy = scipy.signal.medfilt(xc_av,11) + pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) + tck1 = scipy.interpolate.splrep(vels,pred,k=1) + xc_av_orig = xc_av.copy() + xc_av /= pred + vel0_xc = vels[ np.argmin( xc_av ) ] + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ + xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + + xc_av_rough = xc_av + vels_rough = vels + if disp > 30: + disp = 30. + vel_width = np.maximum( 20.0, 6*disp ) + + vels, xc_full, sn, nlines_ccf, W_ccf =\ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.1,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) + pred = scipy.interpolate.splev(vels,tck1) + xc_av /= pred + + if sp_type == 'M5': + moon_sig = 2.5 + elif sp_type == 'K5': + moon_sig = 3.3 + else: + moon_sig = 4.5 + + p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ + sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) + + moonmatters = False + if (know_moon and here_moon): + moonmatters = True + ismoon = True + confused = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) + moon_flag = 1 + else: + confused = False + ismoon = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 + moon_flag = 0 + + bspan = GLOBALutils.calc_bss(vels,xc_av) + SP = bspan[0] + + if (not known_sigma): + disp = np.floor(p1gau[2]) + if (disp < 3.0): + disp = 3.0 + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + known_sigma = True + else: + cond = False + + xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} + + moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ + 'lunation':lunation,'mephem':mephem,'texp':float(h[0].header['EXPTIME'])} + + pkl_xc = dirout + fsim.split('/')[-1][:-8]+obname+'_XC_'+sp_type+'.pkl' + pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) + + ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' + + if not avoid_plot: + GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) + + SNR_5130 = np.median(spec[8,32,900:1101] ) + airmass = float(h[0].header['AIRMASS']) + seeing = -999 + TEXP = float(h[0].header['EXPTIME']) + + if sp_type == 'G2': + if T_eff < 6000: + A = 0.06544 + B = 0.00146 + D = 0.24416 + C = 0.00181 + else: + A = 0.09821 + B = 0.00014 + D = 0.33491 + C = 0.00113 + elif sp_type == 'K5': + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + else: + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + + RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) + depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) + + if depth_fact >= 1.: + RVerr2 = -999.000 + else: + if sp_type == 'G2': + depth_fact = (1 - 0.62) / (1 - depth_fact) + else: + depth_fact = (1 - 0.59) / (1 - depth_fact) + RVerr2 = RVerr * depth_fact + if (RVerr2 <= 0.009): + RVerr2 = 0.009 + + BSerr = D / float(np.round(SNR_5130)) + C + + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) + RVerr2 = np.around(RVerr2,4) + BSerr = np.around(BSerr,4) + + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) + + bjd_out = 2400000.5 + mbjd + T_eff_err = 100 + logg_err = 0.5 + Z_err = 0.5 + vsini_err = 2 + XC_min = np.abs(np.around(np.min(XCmodel),2)) + + SNR_5130 = np.around(SNR_5130) + SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) + + disp_epoch = np.around(p1gau_m[2],1) + hdu = GLOBALutils.update_header(hdu,'RV', RV) + hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) + hdu = GLOBALutils.update_header(hdu,'BS', BS) + hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) + hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) + hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) + hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) + hdu = GLOBALutils.update_header(hdu,'INST', 'FIES') + hdu = GLOBALutils.update_header(hdu,'RESOL', str(resol)) + hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') + hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) + hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) + + # write to output + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f fies ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ + (obname, bjd_out, RV, RVerr2, BS, BSerr, resol, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ + TEXP, SNR_5130_R, ccf_pdf) + f_res.write(line_out) + + if (os.access( dirout + fout,os.F_OK)): + os.remove( dirout + fout) + + hdu.writeto( dirout + fout ) + else: + print("Reading spectral file from", fout) + spec = pyfits.getdata( fout ) + +f_res.close() diff --git a/fies/fiesutils.py b/fies/fiesutils.py index 1e1b750..bd5c010 100644 --- a/fies/fiesutils.py +++ b/fies/fiesutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use("Agg") from astropy.io import fits as pyfits @@ -16,303 +17,303 @@ def get_thar_offsets(lines_thar, order_dir='wavcals/', pref='order_', suf='.iwdat', delt_or=10, del_width=200.,binning=1): - start_or = int(.5*delt_or) - xcs = [] - for ii in range(delt_or,len(lines_thar)-delt_or): - thar_order = lines_thar[ii] - xct = [] - for order in range(ii-start_or,ii+start_or): - order_s = str(order) - if (order < 10): - order_s = '0' + order_s - if os.access(order_dir+pref+order_s+suf,os.F_OK): - f = open(order_dir+pref+order_s+suf,'r') - llins = f.readlines() - if True: - pixel_centers_0 = [] - for line in llins: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])*1./float(binning)) - pixel_centers_0 = np.array(pixel_centers_0).astype('int') - #plot(thar_order) - #plot(pixel_centers_0,thar_order[pixel_centers_0],'ro') - #print order, order_s - #show() - ml = np.array(pixel_centers_0) - 2 - mh = np.array(pixel_centers_0) + 2 - if len(ml)>0: - xc,offs = GLOBALutils.XCorPix( thar_order, ml, mh, del_width=del_width) - else: - xc = np.zeros(len(offs)) - - if len(xct) == 0: - xct = xc.copy() - else: - xct = np.vstack((xct,xc)) - - if len(xcs) == 0: - xcs = xct.copy() - else: - xcs += xct - maxes, maxvels = [],[] - for i in range(xcs.shape[0]): - maxes.append(xcs[i].max()) - maxvels.append(offs[np.argmax(xcs[i])]) - #plot(offs,xcs[i]) - #show() - - maxes,maxvels = np.array(maxes),np.array(maxvels) - orders_offset = -start_or + np.argmax(maxes) - rough_shift = maxvels[np.argmax(maxes)] - - return orders_offset, rough_shift + start_or = int(.5*delt_or) + xcs = [] + for ii in range(delt_or,len(lines_thar)-delt_or): + thar_order = lines_thar[ii] + xct = [] + for order in range(ii-start_or,ii+start_or): + order_s = str(order) + if (order < 10): + order_s = '0' + order_s + if os.access(order_dir+pref+order_s+suf,os.F_OK): + f = open(order_dir+pref+order_s+suf,'r') + llins = f.readlines() + if True: + pixel_centers_0 = [] + for line in llins: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])*1./float(binning)) + pixel_centers_0 = np.array(pixel_centers_0).astype('int') + #plot(thar_order) + #plot(pixel_centers_0,thar_order[pixel_centers_0],'ro') + #print order, order_s + #show() + ml = np.array(pixel_centers_0) - 2 + mh = np.array(pixel_centers_0) + 2 + if len(ml)>0: + xc,offs = GLOBALutils.XCorPix( thar_order, ml, mh, del_width=del_width) + else: + xc = np.zeros(len(offs)) + + if len(xct) == 0: + xct = xc.copy() + else: + xct = np.vstack((xct,xc)) + + if len(xcs) == 0: + xcs = xct.copy() + else: + xcs += xct + maxes, maxvels = [],[] + for i in range(xcs.shape[0]): + maxes.append(xcs[i].max()) + maxvels.append(offs[np.argmax(xcs[i])]) + #plot(offs,xcs[i]) + #show() + + maxes,maxvels = np.array(maxes),np.array(maxvels) + orders_offset = -start_or + np.argmax(maxes) + rough_shift = maxvels[np.argmax(maxes)] + + return orders_offset, rough_shift def ra_from_sec(ra,time=True): - ra = float(ra) - sign = ' ' - if ra < 0: - sign = '-' - ra *= -1 - - hh = ra/3600. - mm = (hh - int(hh))*60. - ss = (mm - int(mm))*60. - shh = str(int(hh)) - smm = str(int(mm)) - sss = str(np.around(ss,2)) - if hh<10: - shh = '0' + shh - if mm<10: - smm = '0' + smm - if ss<10: - sss = '0' + sss - return sign + shh + ':' + smm + ':' + sss + ra = float(ra) + sign = ' ' + if ra < 0: + sign = '-' + ra *= -1 + + hh = ra/3600. + mm = (hh - int(hh))*60. + ss = (mm - int(mm))*60. + shh = str(int(hh)) + smm = str(int(mm)) + sss = str(np.around(ss,2)) + if hh<10: + shh = '0' + shh + if mm<10: + smm = '0' + smm + if ss<10: + sss = '0' + sss + return sign + shh + ':' + smm + ':' + sss def FileClassify(diri, log,binning=1,mode='F1', dark_corr=False): - """ - - Classifies all files in a directory and writes a night log of science images - - """ - - # define output lists - sim_sci = [] - biases = [] - flats = [] - ThAr_ref = [] - ThAr_ref_dates = [] - ThAr_co = [] - ThAr_co_dates = [] - ThAr_sim = [] - ThAr_sim_dates = [] - flat_ref_dates = [] - bias_ref_dates = [] - obnames = [] - exptimes = [] - darks = [] - flats_co = [] - flats_co_dates = [] - - sdarks = [] - if dark_corr and os.access(diri+'/darks.txt',os.F_OK): - fd = open(diri+'/darks.txt','r') - ds = fd.readlines() - for dk in ds: - sdarks.append(diri+dk[:-1]) - sdarks = np.array(sdarks) - - f = open(log,'w') - - #Do not consider the images specified in dir+badfiles.txt - bad_files = [] - if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - - all_files = glob.glob(diri+"/*fits") - for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - isdark=False - for df in sdarks: - if archivo == df: - darks.append(archivo) - isdark=True - - if dump == False and isdark == False: - print archivo - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - if int(h[0].header['DETXBIN']) == binning and int(h[0].header['DETYBIN']) == binning and (mode in h[0].header['FIFMSKNM']) and h[0].header['IMAGETYP'] != 'COUNTTEST': - print archivo, h[0].header['IMAGETYP'], h[0].header['SHSTAT'], h[0].header['EXPTIME'], h[0].header['OBJECT'], h[0].header['TCSTGT'], int(h[0].header['DETYBIN']) - - if h[0].header['IMAGETYP'] == 'BIAS': - biases.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - bias_ref_dates.append( mjd ) - elif h[0].header['IMAGETYP'] == 'FLAT': - flats.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - flat_ref_dates.append( mjd ) - if h[0].header['FICARMID'] == 6 and h[0].header['FILMP1'] == 1 and h[0].header['FILMP6']==0: - flats_co.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - flats_co_dates.append( mjd ) - else: - flats.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - flat_ref_dates.append( mjd ) - sc = pyfits.getdata(archivo) - #plot(sc[1000]) - elif h[0].header['IMAGETYP'] == 'WAVE': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - ThAr_ref_dates.append( mjd ) - - - elif ((mode=='F3' or mode=='F4') and h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 0 and h[0].header['FILMP7']==1)\ - or (mode=='F1' and h[0].header['FICARMID'] == 2 and h[0].header['FILMP4'] == 0 and h[0].header['FILMP7']==1): - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - ThAr_ref_dates.append( mjd ) - - elif h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 1 and h[0].header['FILMP7']==0: - ThAr_co.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - ThAr_co_dates.append( mjd ) - - elif h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 1 and h[0].header['FILMP7']==1: - ThAr_sim.append(archivo) - mjd, mjd0 = mjd_fromheader2(h) - ThAr_sim_dates.append( mjd ) - - elif (mode=='F3' and h[0].header['FICARMID'] == 2) or (mode == 'F1' and h[0].header['FICARMID'] == 5)\ - or (mode=='F4' and (h[0].header['FICARMID'] == 5 or h[0].header['FICARMID'] == 4)): - sim_sci.append(archivo) - obname = h[0].header['OBJECT'] - obnames.append( obname ) - ra = ra_from_sec(h[0].header['RA']*3600.*24./360.) - delta = ra_from_sec(h[0].header['DEC']*3600.) - airmass= float(h[0].header['AIRMASS']) - texp = float(h[0].header['EXPTIME']) - - date = h[0].header['DATE-OBS'] - hour = date[11:] - date = date[:10] - exptimes.append( texp ) - if h[0].header['FILMP4'] == 1: - simult = 'SIMULT' - else: - simult = 'NO_SIMULT' - line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo, simult) - f.write(line) - - #show() - flat_ref_dates = np.array(flat_ref_dates) - flats = np.array(flats) - IS = np.argsort(flat_ref_dates) - flat_ref_dates = flat_ref_dates[IS] - flats = flats[IS] - #for i in range(len(flats)): - # print 'flat',flats[i], flat_ref_dates[i] - - bias_ref_dates = np.array(bias_ref_dates) - biases = np.array(biases) - IS = np.argsort(bias_ref_dates) - bias_ref_dates = bias_ref_dates[IS] - biases = biases[IS] - #for i in range(len(biases)): - # print 'bias',biases[i], bias_ref_dates[i] - f.close() - - return biases, np.array(flats), np.array(ThAr_ref), sim_sci, np.array(ThAr_ref_dates), obnames, exptimes, np.array(darks), np.array(flats_co), np.array(flats_co_dates),np.array(ThAr_sim), np.array(ThAr_sim_dates),np.array(ThAr_co), np.array(ThAr_co_dates) + """ + + Classifies all files in a directory and writes a night log of science images + + """ + + # define output lists + sim_sci = [] + biases = [] + flats = [] + ThAr_ref = [] + ThAr_ref_dates = [] + ThAr_co = [] + ThAr_co_dates = [] + ThAr_sim = [] + ThAr_sim_dates = [] + flat_ref_dates = [] + bias_ref_dates = [] + obnames = [] + exptimes = [] + darks = [] + flats_co = [] + flats_co_dates = [] + + sdarks = [] + if dark_corr and os.access(diri+'/darks.txt',os.F_OK): + fd = open(diri+'/darks.txt','r') + ds = fd.readlines() + for dk in ds: + sdarks.append(diri+dk[:-1]) + sdarks = np.array(sdarks) + + f = open(log,'w') + + #Do not consider the images specified in dir+badfiles.txt + bad_files = [] + if os.access(diri+'bad_files.txt',os.F_OK): + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + + all_files = glob.glob(diri+"/*fits") + for archivo in all_files: + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + isdark=False + for df in sdarks: + if archivo == df: + darks.append(archivo) + isdark=True + + if dump == False and isdark == False: + print(archivo) + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + if int(h[0].header['DETXBIN']) == binning and int(h[0].header['DETYBIN']) == binning and (mode in h[0].header['FIFMSKNM']) and h[0].header['IMAGETYP'] != 'COUNTTEST': + print(archivo, h[0].header['IMAGETYP'], h[0].header['SHSTAT'], h[0].header['EXPTIME'], h[0].header['OBJECT'], h[0].header['TCSTGT'], int(h[0].header['DETYBIN'])) + + if h[0].header['IMAGETYP'] == 'BIAS': + biases.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + bias_ref_dates.append( mjd ) + elif h[0].header['IMAGETYP'] == 'FLAT': + flats.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + flat_ref_dates.append( mjd ) + if h[0].header['FICARMID'] == 6 and h[0].header['FILMP1'] == 1 and h[0].header['FILMP6']==0: + flats_co.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + flats_co_dates.append( mjd ) + else: + flats.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + flat_ref_dates.append( mjd ) + sc = pyfits.getdata(archivo) + #plot(sc[1000]) + elif h[0].header['IMAGETYP'] == 'WAVE': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + ThAr_ref_dates.append( mjd ) + + + elif ((mode=='F3' or mode=='F4') and h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 0 and h[0].header['FILMP7']==1)\ + or (mode=='F1' and h[0].header['FICARMID'] == 2 and h[0].header['FILMP4'] == 0 and h[0].header['FILMP7']==1): + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + ThAr_ref_dates.append( mjd ) + + elif h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 1 and h[0].header['FILMP7']==0: + ThAr_co.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + ThAr_co_dates.append( mjd ) + + elif h[0].header['FICARMID'] == 6 and h[0].header['FILMP4'] == 1 and h[0].header['FILMP7']==1: + ThAr_sim.append(archivo) + mjd, mjd0 = mjd_fromheader2(h) + ThAr_sim_dates.append( mjd ) + + elif (mode=='F3' and h[0].header['FICARMID'] == 2) or (mode == 'F1' and h[0].header['FICARMID'] == 5)\ + or (mode=='F4' and (h[0].header['FICARMID'] == 5 or h[0].header['FICARMID'] == 4)): + sim_sci.append(archivo) + obname = h[0].header['OBJECT'] + obnames.append( obname ) + ra = ra_from_sec(h[0].header['RA']*3600.*24./360.) + delta = ra_from_sec(h[0].header['DEC']*3600.) + airmass= float(h[0].header['AIRMASS']) + texp = float(h[0].header['EXPTIME']) + + date = h[0].header['DATE-OBS'] + hour = date[11:] + date = date[:10] + exptimes.append( texp ) + if h[0].header['FILMP4'] == 1: + simult = 'SIMULT' + else: + simult = 'NO_SIMULT' + line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo, simult) + f.write(line) + + #show() + flat_ref_dates = np.array(flat_ref_dates) + flats = np.array(flats) + IS = np.argsort(flat_ref_dates) + flat_ref_dates = flat_ref_dates[IS] + flats = flats[IS] + #for i in range(len(flats)): + # print 'flat',flats[i], flat_ref_dates[i] + + bias_ref_dates = np.array(bias_ref_dates) + biases = np.array(biases) + IS = np.argsort(bias_ref_dates) + bias_ref_dates = bias_ref_dates[IS] + biases = biases[IS] + #for i in range(len(biases)): + # print 'bias',biases[i], bias_ref_dates[i] + f.close() + + return biases, np.array(flats), np.array(ThAr_ref), sim_sci, np.array(ThAr_ref_dates), obnames, exptimes, np.array(darks), np.array(flats_co), np.array(flats_co_dates),np.array(ThAr_sim), np.array(ThAr_sim_dates),np.array(ThAr_co), np.array(ThAr_co_dates) def get_darktimes(darks): - times = [] - for dark in darks: - hd = pyfits.getheader(dark) - times.append(hd['EXPTIME']) - return np.unique(np.sort(np.array(times))), np.array(times) + times = [] + for dark in darks: + hd = pyfits.getheader(dark) + times.append(hd['EXPTIME']) + return np.unique(np.sort(np.array(times))), np.array(times) def mjd_fromheader2(h): - """ - return modified Julian date from header - """ + """ + return modified Julian date from header + """ - datetu = h[0].header['DATE-OBS'] - mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10])) - ho = int(datetu[11:13]) - mi = int(datetu[14:16]) - se = float(datetu[17:]) - ut = float(ho) + float(mi)/60.0 + float(se)/3600.0 - mjd_start = mjd + ut/24.0 + datetu = h[0].header['DATE-OBS'] + mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10])) + ho = int(datetu[11:13]) + mi = int(datetu[14:16]) + se = float(datetu[17:]) + ut = float(ho) + float(mi)/60.0 + float(se)/3600.0 + mjd_start = mjd + ut/24.0 - secinday = 24*3600.0 - fraction = 0.5 - texp = h[0].header['EXPTIME'] #sec + secinday = 24*3600.0 + fraction = 0.5 + texp = h[0].header['EXPTIME'] #sec - mjd = mjd_start + (fraction * texp) / secinday + mjd = mjd_start + (fraction * texp) / secinday - return mjd, mjd0 + return mjd, mjd0 def get_RONGAIN(hd): - return hd['RDNOISE'], hd['GAIN'] + return hd['RDNOISE'], hd['GAIN'] def MedianCombine(ImgList, zero='none', binning=1, oii=100, off=2148): - """ - Median combine a list of images - """ - - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - - h = pyfits.open(ImgList[0]) - - d1 = h[1].data - h1 = h[1].header - d1 = OverscanTrim(d1, binning=binning,ii=oii, ff=off) - if zero != 'none': - z = pyfits.open(zero)[0] - d1 -= z.data - - factor = 1.25 - if (n < 3): - factor = 1 - - ron1,gain1 = get_RONGAIN(h[1].header) - - ron1 = factor * ron1 / np.sqrt(n) - if n>1: - for i in range(n-1): - td = pyfits.open(ImgList[i+1]) - if zero == 'none': - d1 = np.dstack((d1,OverscanTrim(td[1].data, binning=binning, ii=oii, ff=off))) - - else: - d1 = np.dstack((d1,OverscanTrim(td[1].data, binning=binning, ii=oii, ff=off)-z.data)) - d1 = np.median(d1,axis=2) - return d1, ron1, gain1 + """ + Median combine a list of images + """ + + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + + h = pyfits.open(ImgList[0]) + + d1 = h[1].data + h1 = h[1].header + d1 = OverscanTrim(d1, binning=binning,ii=oii, ff=off) + if zero != 'none': + z = pyfits.open(zero)[0] + d1 -= z.data + + factor = 1.25 + if (n < 3): + factor = 1 + + ron1,gain1 = get_RONGAIN(h[1].header) + + ron1 = factor * ron1 / np.sqrt(n) + if n>1: + for i in range(n-1): + td = pyfits.open(ImgList[i+1]) + if zero == 'none': + d1 = np.dstack((d1,OverscanTrim(td[1].data, binning=binning, ii=oii, ff=off))) + + else: + d1 = np.dstack((d1,OverscanTrim(td[1].data, binning=binning, ii=oii, ff=off)-z.data)) + d1 = np.median(d1,axis=2) + return d1, ron1, gain1 def OverscanTrim(dat,binning=1,ii=100,ff=2148): - """ - Overscan correct and Trim a refurbished FEROS image - """ - #ff = 2098 - #ii = 50 - ff = int(np.around(ff/binning)) - ii = int(np.around(ii/binning)) - os = dat[:,ff:] - s = np.median(os) - newdata = dat[:,ii:ff].copy() - s - - return newdata \ No newline at end of file + """ + Overscan correct and Trim a refurbished FEROS image + """ + #ff = 2098 + #ii = 50 + ff = int(np.around(ff/binning)) + ii = int(np.around(ii/binning)) + os = dat[:,ff:] + s = np.median(os) + newdata = dat[:,ii:ff].copy() - s + + return newdata diff --git a/harps/groups.py b/harps/groups.py index 823b8f5..e00bf0a 100644 --- a/harps/groups.py +++ b/harps/groups.py @@ -1,37 +1,38 @@ +from __future__ import print_function import os from pylab import * f = open('/data/echelle/harps/list.txt','r') lines = f.readlines() rv,rve,jd,refs = [],[],[],[] for line in lines: - path = line[:-1] - #line = 'python ferospipe.py '+path+' -npools 16 -do_class -avoid_plot -o2do HD157347 -reffile /data/echelle/feros/reffile.txt' - line = 'python harpspipe.py '+path+' -npools 10 -do_class -avoid_plot' - os.system(line) - """ - pr = path[:-1]+'_red/proc/results.txt' + path = line[:-1] + #line = 'python ferospipe.py '+path+' -npools 16 -do_class -avoid_plot -o2do HD157347 -reffile /data/echelle/feros/reffile.txt' + line = 'python harpspipe.py '+path+' -npools 10 -do_class -avoid_plot' + os.system(line) + """ + pr = path[:-1]+'_red/proc/results.txt' + + try: + fo = open(pr,'r') + lines2 = fo.readlines() + for line2 in lines2: + cos = line2.split() + if float(cos[16])>50 and cos[0]=='HD157347': + print line2[:-1] + rv.append(float(cos[2])) + rve.append(float(cos[3])) + jd.append(float(cos[1])) + st = path[:-1] + st = st.split('/')[-1] + mo = float(st[4:6]) + da = float(st[6:]) + ref = mo + da/30.5 + refs.append(ref) + except: + print 'bad' + + - try: - fo = open(pr,'r') - lines2 = fo.readlines() - for line2 in lines2: - cos = line2.split() - if float(cos[16])>50 and cos[0]=='HD157347': - print line2[:-1] - rv.append(float(cos[2])) - rve.append(float(cos[3])) - jd.append(float(cos[1])) - st = path[:-1] - st = st.split('/')[-1] - mo = float(st[4:6]) - da = float(st[6:]) - ref = mo + da/30.5 - refs.append(ref) - except: - print 'bad' - - - jd,rv,refs = np.array(jd),np.array(rv),np.array(refs) I = np.where(refs<20)[0] print np.sqrt(np.var(rv[I])) diff --git a/harps/harpspipe.py b/harps/harpspipe.py index ea411c2..7482f77 100644 --- a/harps/harpspipe.py +++ b/harps/harpspipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * base = '../' @@ -59,7 +60,7 @@ npools = int(args.npools) object2do = args.o2do reffile = args.reffile -mode = args.mode +mode = args.mode if dirin[-1] != '/': dirin = dirin + '/' @@ -103,7 +104,7 @@ NSigma_Marsh = 5 NCosmic_Marsh = 5 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 0 max_extract_col = 4095 @@ -131,44 +132,44 @@ ############################# -print "\n\n\tHARPS ESO3.6m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tHARPS ESO3.6m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # file containing the log log = dirout+'night.log' biases, flats, ob_flats, co_flats, ThAr_ref, sim_sci, ThAr_ref_dates, obnames, exptimes, co_types = harpsutils.FileClassify(dirin,log,mode=mode) -print '\tThis in the log of the night:\n' +print('\tThis in the log of the night:\n') f = open(log) flines = f.readlines() for line in flines: - print '\t'+line[:-1] -print '\n' + print('\t'+line[:-1]) +print('\n') if ( (os.access(dirout+'FlatOb_'+ mode +'.fits',os.F_OK) == False and len(ob_flats)!=0) or \ (os.access(dirout+'FlatCo_'+ mode +'.fits',os.F_OK) == False and len(co_flats)!=0) or \ - (os.access(dirout+'Flat_'+ mode +'.fits', os.F_OK) == False and len(flats)!=0) or \ + (os.access(dirout+'Flat_'+ mode +'.fits', os.F_OK) == False and len(flats)!=0) or \ (os.access(dirout+'trace_'+ mode +'.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias_'+ mode +'.fits',os.F_OK) == False and len(biases)!=0) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine Biases - print "\t\tGenerating Master calibration frames..." + print("\t\tGenerating Master calibration frames...") MasterBias, RO_bias, GA_bias = harpsutils.MedianCombine(biases) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias_'+mode+'.fits',os.F_OK)): os.remove(dirout+'MasterBias_'+mode+'.fits') hdu.writeto(dirout+'MasterBias_'+mode+'.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") # median combine list of ob flats Flat_ob, RO_ob, GA_ob = harpsutils.MedianCombine(ob_flats, zero=dirout+'MasterBias_'+mode+'.fits') @@ -191,19 +192,19 @@ if (os.access(dirout+'Flat_'+mode+'.fits',os.F_OK)): os.remove(dirout+'Flat_'+mode+'.fits') hdu.writeto(dirout+'Flat_'+mode+'.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all1,nord_all1 = GLOBALutils.get_them(Flat[:,:,0],ext_aperture_B,trace_degree,mode=1) c_all1 = c_all1[5:] - nord_all1 = len(c_all1) + nord_all1 = len(c_all1) I = np.arange(0,nord_all1,2).astype('int') c_ob1 = c_all1[I] nord_ob1 = len(I) I = np.arange(1,nord_all1,2).astype('int') c_co1 = c_all1[I] nord_co1 = len(I) - + c_all2,nord_all2 = GLOBALutils.get_them(Flat[:,:,1],ext_aperture_R,trace_degree,mode=1) I = np.arange(0,nord_all2,2).astype('int') c_ob2 = c_all2[I] @@ -212,22 +213,22 @@ c_co2 = c_all2[I] nord_co2 = len(I) - print "\t\t"+str(nord_ob1)+" object orders found in blue chip..." - print "\t\t"+str(nord_co1)+" comparison orders found in blue chip..." - print "\t\t"+str(nord_ob2)+" object orders found in red chip..." - print "\t\t"+str(nord_co2)+" comparison orders found in red chip..." + print("\t\t"+str(nord_ob1)+" object orders found in blue chip...") + print("\t\t"+str(nord_co1)+" comparison orders found in blue chip...") + print("\t\t"+str(nord_ob2)+" object orders found in red chip...") + print("\t\t"+str(nord_co2)+" comparison orders found in red chip...") # pickle traces trace_dict = {'c_ob1':c_ob1, 'c_co1':c_co1, - 'c_ob2':c_ob2, + 'c_ob2':c_ob2, 'c_co2':c_co2, - 'c_all1':c_all1, - 'c_all2':c_all2, + 'c_all1':c_all1, + 'c_all2':c_all2, 'nord_ob1':nord_ob1, 'nord_co1':nord_co1, - 'nord_ob2':nord_ob2, 'nord_co2':nord_co2, + 'nord_ob2':nord_ob2, 'nord_co2':nord_co2, 'GA_ob': GA_ob, 'RO_ob': RO_ob, 'GA_co': GA_co, 'RO_co': RO_co, - 'GA_fl': GA_fl, 'RO_fl': RO_fl} + 'GA_fl': GA_fl, 'RO_fl': RO_fl} pickle.dump( trace_dict, open( dirout+"trace_"+mode+".pkl", 'w' ) ) else: @@ -256,13 +257,13 @@ Flat = h[0].data h = pyfits.open(dirout+'MasterBias_'+mode+'.fits') MasterBias = h[0].data - + # mesh all orders c_all1 = GLOBALutils.Mesh( c_ob1, c_co1) c_all2 = GLOBALutils.Mesh( c_ob2, c_co2) # Extract flat spectra, object -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') P_ob_B_fits = dirout + 'P_ob_B_'+mode+'.fits' P_ob_R_fits = dirout + 'P_ob_R_'+mode+'.fits' B_flat_ob_fits = dirout +'B_flat_ob_'+mode+'.fits' @@ -274,7 +275,7 @@ R_flat_ob = np.zeros((nord_ob2, 3, Flat_ob.shape[1]) ) if ( os.access(P_ob_B_fits,os.F_OK) == False ) or ( os.access(B_flat_ob_fits,os.F_OK) == False ) or ( os.access(P_ob_R_fits,os.F_OK) == False ) or ( os.access(R_flat_ob_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo extracted flat object spectra found or extraction forced, extracting and saving..." + print("\t\tNo extracted flat object spectra found or extraction forced, extracting and saving...") bacfile = dirout + 'BACR_FLAT_'+mode+'.fits' if os.access(bacfile,os.F_OK) == False: @@ -290,7 +291,7 @@ bacR = pyfits.getdata(bacfile) flR = Flat_ob[:,:,1] - bacR - + bacfile = dirout + 'BACB_FLAT_'+mode+'.fits' if os.access(bacfile,os.F_OK) == False: CentersB = np.zeros((len(c_ob1),Flat_ob[:,:,0].shape[1])) @@ -308,31 +309,31 @@ P_ob_B = GLOBALutils.obtain_P(flB,c_ob1,ext_aperture_B,RO_fl[0],\ GA_fl[0],NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) P_co_B = GLOBALutils.obtain_P(flB,c_co1,ext_aperture_B,RO_fl[0],\ GA_fl[0],NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) P_ob_R = GLOBALutils.obtain_P(flR,c_ob2,ext_aperture_R,RO_fl[1],\ GA_fl[0],NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) P_co_R = GLOBALutils.obtain_P(flR,c_co2,ext_aperture_R,RO_fl[1],\ GA_fl[0],NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) - print "\t\t\tWill extract",nord_ob1,"blue orders" + print("\t\t\tWill extract",nord_ob1,"blue orders") B_flat_ob = GLOBALutils.optimal_extraction(flB,P_ob_B,c_ob1,ext_aperture_B,RO_fl[0],GA_fl[0],\ S_Marsh,NCosmic_Marsh*10,min_extract_col,\ max_extract_col,npools) B_flat_ob = GLOBALutils.invert(B_flat_ob) - print "\t\t\tWill extract",nord_ob2,"red orders" + print("\t\t\tWill extract",nord_ob2,"red orders") R_flat_ob = GLOBALutils.optimal_extraction(flR,P_ob_R,c_ob2,ext_aperture_R,RO_fl[1],GA_fl[1],\ S_Marsh,NCosmic_Marsh*10,min_extract_col,\ @@ -350,7 +351,7 @@ os.remove( P_ob_R_fits ) if (os.access(R_flat_ob_fits,os.F_OK)): os.remove( R_flat_ob_fits ) - + hdu = pyfits.PrimaryHDU( P_ob_B ) hdu.writeto( P_ob_B_fits ) hdu = pyfits.PrimaryHDU( P_ob_R ) @@ -361,7 +362,7 @@ hdu.writeto( R_flat_ob_fits ) else: - print "\t\tExtracted flat object spectra found, loading..." + print("\t\tExtracted flat object spectra found, loading...") P_ob_B = pyfits.getdata( P_ob_B_fits ) P_ob_R = pyfits.getdata( P_ob_R_fits ) B_flat_ob = pyfits.getdata( B_flat_ob_fits ) @@ -379,8 +380,8 @@ R_flat_co = np.zeros((nord_co2, 3, Flat_co.shape[1]) ) if ( os.access(P_co_B_fits,os.F_OK) == False ) or ( os.access(B_flat_co_fits,os.F_OK) == False ) or ( os.access(P_co_R_fits,os.F_OK) == False ) or ( os.access(R_flat_co_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving..." - + print("\t\tNo extracted flat comparison spectra found or extraction forced, extracting and saving...") + bacfile = dirout + 'BACR_FLAT_CO_'+mode+'.fits' if os.access(bacfile,os.F_OK) == False: CentersR = np.zeros((len(c_co2),Flat_co[:,:,1].shape[1])) @@ -395,7 +396,7 @@ bacR = pyfits.getdata(bacfile) flR = Flat_co[:,:,1] - bacR - + bacfile = dirout + 'BACB_FLAT_CO_'+mode+'.fits' if os.access(bacfile,os.F_OK) == False: CentersB = np.zeros((len(c_co1),Flat_co[:,:,0].shape[1])) @@ -411,10 +412,10 @@ flB = Flat_co[:,:,0] - bacB - print "\t\t\tWill extract",nord_co1,"blue orders" + print("\t\t\tWill extract",nord_co1,"blue orders") for i in range(nord_co1): P_marsh = GLOBALutils.PCoeff( flB, c_co1[i,:], ext_aperture_B, RO_co[0], GA_co[0], NSigma_Marsh,\ - S_Marsh, N_Marsh, Marsh_alg, min_extract_col,max_extract_col ) + S_Marsh, N_Marsh, Marsh_alg, min_extract_col,max_extract_col ) P_co_B += P_marsh B_flat_co = GLOBALutils.optimal_extraction(Flat[:,:,0],P_co_B,c_co1,ext_aperture_B,RO_fl[0],GA_fl[0],\ @@ -422,10 +423,10 @@ max_extract_col,npools) B_flat_co = GLOBALutils.invert(B_flat_co) - print "\t\t\tWill extract",nord_co2,"red orders" + print("\t\t\tWill extract",nord_co2,"red orders") for i in range(nord_co2): P_marsh = GLOBALutils.PCoeff( flR, c_co2[i,:], ext_aperture_R, RO_co[1], GA_co[1], NSigma_Marsh,\ - S_Marsh, N_Marsh, Marsh_alg, min_extract_col,max_extract_col ) + S_Marsh, N_Marsh, Marsh_alg, min_extract_col,max_extract_col ) P_co_R += P_marsh R_flat_co = GLOBALutils.optimal_extraction(Flat[:,:,1],P_co_R,c_co2,ext_aperture_R,RO_fl[1],GA_fl[1],\ @@ -444,7 +445,7 @@ os.remove( P_co_R_fits ) if (os.access(R_flat_co_fits,os.F_OK)): os.remove( R_flat_co_fits ) - + hdu = pyfits.PrimaryHDU( P_co_B ) hdu.writeto( P_co_B_fits ) hdu = pyfits.PrimaryHDU( P_co_R ) @@ -455,7 +456,7 @@ hdu.writeto( R_flat_co_fits ) else: - print "\t\tExtracted flat comparison spectra found, loading..." + print("\t\tExtracted flat comparison spectra found, loading...") P_co_B = pyfits.getdata( P_co_B_fits ) P_co_R = pyfits.getdata( P_co_R_fits ) B_flat_co = pyfits.getdata( B_flat_co_fits ) @@ -466,16 +467,16 @@ B_flat_ob_n,Bnorms = GLOBALutils.FlatNormalize_single( B_flat_ob, mid=2048) R_flat_ob_n,Rnorms = GLOBALutils.FlatNormalize_single( R_flat_ob, mid=2048) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in ThAr_ref: - print "\t\tWorking on ThAr+Ne file ", fsim, "..." + print("\t\tWorking on ThAr+Ne file ", fsim, "...") hthar = pyfits.open( fsim ) dtharB = harpsutils.OverscanTrim( hthar[1].data ) - MasterBias[:,:,0] dtharR = harpsutils.OverscanTrim( hthar[2].data ) - MasterBias[:,:,1] - print 'one' + print('one') bacfile = dirout + 'BACR_' + fsim.split('/')[-1][:-4]+'fits' - + if os.access(bacfile,os.F_OK) == False: CentersR = np.zeros((len(c_all2),dtharR.shape[1])) for i in range(len(c_all2)): @@ -488,7 +489,7 @@ hdbac.writeto(bacfile) else: bacR = pyfits.getdata(bacfile) - print 'two' + print('two') bacfile = dirout + 'BACB_' + fsim.split('/')[-1][:-4]+'fits' if os.access(bacfile,os.F_OK) == False: CentersB = np.zeros((len(c_all1),dtharB.shape[1])) @@ -512,39 +513,39 @@ if ( os.access(thar_fits_ob_B,os.F_OK) == False ) or ( os.access(thar_fits_co_B,os.F_OK) == False ) \ or ( os.access(thar_fits_ob_R,os.F_OK) == False ) or ( os.access(thar_fits_co_R,os.F_OK) == False ) \ or ( force_thar_extract ): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") - tR,tG = hthar[1].header['HIERARCH ESO DET OUT1 RON'],hthar[1].header['HIERARCH ESO DET OUT1 GAIN'] + tR,tG = hthar[1].header['HIERARCH ESO DET OUT1 RON'],hthar[1].header['HIERARCH ESO DET OUT1 GAIN'] - thar_S_ob_B = GLOBALutils.optimal_extraction(dtharB,P_ob_B,c_ob1,ext_aperture_B,tR,tG,\ + thar_S_ob_B = GLOBALutils.optimal_extraction(dtharB,P_ob_B,c_ob1,ext_aperture_B,tR,tG,\ S_Marsh,0*NCosmic_Marsh,min_extract_col,max_extract_col,npools) for i in range(nord_ob1): - thar_S_ob_B[i,1,:] = thar_S_ob_B[i,1,:][::-1] - thar_S_ob_B[i,2,:] = thar_S_ob_B[i,2,:][::-1] + thar_S_ob_B[i,1,:] = thar_S_ob_B[i,1,:][::-1] + thar_S_ob_B[i,2,:] = thar_S_ob_B[i,2,:][::-1] - thar_S_co_B = GLOBALutils.optimal_extraction(dtharB,P_co_B,c_co1,ext_aperture_B,tR,tG,\ + thar_S_co_B = GLOBALutils.optimal_extraction(dtharB,P_co_B,c_co1,ext_aperture_B,tR,tG,\ S_Marsh,0*NCosmic_Marsh,min_extract_col,max_extract_col,npools) for i in range(nord_co1): - thar_S_co_B[i,1,:] = thar_S_co_B[i,1,:][::-1] - thar_S_co_B[i,2,:] = thar_S_co_B[i,2,:][::-1] + thar_S_co_B[i,1,:] = thar_S_co_B[i,1,:][::-1] + thar_S_co_B[i,2,:] = thar_S_co_B[i,2,:][::-1] - thar_S_ob_B,thar_S_co_B = thar_S_ob_B[::-1],thar_S_co_B[::-1] + thar_S_ob_B,thar_S_co_B = thar_S_ob_B[::-1],thar_S_co_B[::-1] - tR,tG = hthar[2].header['HIERARCH ESO DET OUT1 RON'],hthar[2].header['HIERARCH ESO DET OUT1 GAIN'] + tR,tG = hthar[2].header['HIERARCH ESO DET OUT1 RON'],hthar[2].header['HIERARCH ESO DET OUT1 GAIN'] - thar_S_ob_R = GLOBALutils.optimal_extraction(dtharR,P_ob_R,c_ob2,ext_aperture_R,tR,tG,\ + thar_S_ob_R = GLOBALutils.optimal_extraction(dtharR,P_ob_R,c_ob2,ext_aperture_R,tR,tG,\ S_Marsh,0*NCosmic_Marsh,min_extract_col,max_extract_col,npools) for i in range(nord_ob2): - thar_S_ob_R[i,1,:] = thar_S_ob_R[i,1,:][::-1] - thar_S_ob_R[i,2,:] = thar_S_ob_R[i,2,:][::-1] + thar_S_ob_R[i,1,:] = thar_S_ob_R[i,1,:][::-1] + thar_S_ob_R[i,2,:] = thar_S_ob_R[i,2,:][::-1] - thar_S_co_R = GLOBALutils.optimal_extraction(dtharR,P_co_R,c_co2,ext_aperture_R,tR,tG,\ + thar_S_co_R = GLOBALutils.optimal_extraction(dtharR,P_co_R,c_co2,ext_aperture_R,tR,tG,\ S_Marsh,0*NCosmic_Marsh,min_extract_col,max_extract_col,npools) for i in range(nord_co2): - thar_S_co_R[i,1,:] = thar_S_co_R[i,1,:][::-1] - thar_S_co_R[i,2,:] = thar_S_co_R[i,2,:][::-1] + thar_S_co_R[i,1,:] = thar_S_co_R[i,1,:][::-1] + thar_S_co_R[i,2,:] = thar_S_co_R[i,2,:][::-1] - thar_S_ob_R,thar_S_co_R = thar_S_ob_R[::-1],thar_S_co_R[::-1] + thar_S_ob_R,thar_S_co_R = thar_S_ob_R[::-1],thar_S_co_R[::-1] # save as fits file if (os.access(thar_fits_ob_R,os.F_OK)): @@ -555,7 +556,7 @@ os.remove( thar_fits_co_R ) if (os.access(thar_fits_co_B,os.F_OK)): os.remove( thar_fits_co_B ) - + hdu = pyfits.PrimaryHDU( thar_S_ob_B ) hdu.writeto( thar_fits_ob_B ) hdu = pyfits.PrimaryHDU( thar_S_ob_R ) @@ -565,10 +566,10 @@ hdu = pyfits.PrimaryHDU( thar_S_co_R ) hdu.writeto( thar_fits_co_R ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") # create wavelength calibration files -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") sorted_ThAr_dates = np.argsort( ThAr_ref_dates ) for i in range(len(ThAr_ref_dates)): @@ -577,7 +578,7 @@ wavsol_fits = dirout + ThAr_ref[index].split('/')[-1][:-4]+'spec.fits' #force_thar_wavcal = True if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on initial ThAr file", ThAr_ref[index] + print("\t\tWorking on initial ThAr file", ThAr_ref[index]) hthar = pyfits.open( ThAr_ref[index] ) mjd, mjd0 = harpsutils.mjd_fromheader( hthar ) thar_fits_ob_B = dirout + ThAr_ref[index].split('/')[-1][:-4]+'spec.ob.B.fits.S' @@ -598,322 +599,322 @@ iv_thar_ob_R = thar_S_ob_R[:,2,:] lines_thar_co_R = thar_S_co_R[:,1,:] iv_thar_co_R = thar_S_co_R[:,2,:] - - c_p2w_ob_B = np.zeros((nord_ob1,porder+1)) - c_p2w_ob_R = np.zeros((nord_ob2,porder+1)) - c_p2w_co_B = np.zeros((nord_co1,porder+1)) - c_p2w_co_R = np.zeros((nord_co2,porder+1)) - spec_thar_ob = np.zeros((2,nord_ob1+nord_ob2,thar_S_ob_B.shape[2])) - - All_Pixel_Centers_R = np.array([]) - All_Wavelengths_R = np.array([]) - All_Orders_R = np.array([]) - All_Centroids_R = np.array([]) - All_Sigmas_R = np.array([]) - All_Intensities_R = np.array([]) - - counter = 0 - temp_pix = np.array([]) - temp_res = np.array([]) - meds,ords = [],[] - for order in range(nord_ob2): + + c_p2w_ob_B = np.zeros((nord_ob1,porder+1)) + c_p2w_ob_R = np.zeros((nord_ob2,porder+1)) + c_p2w_co_B = np.zeros((nord_co1,porder+1)) + c_p2w_co_R = np.zeros((nord_co2,porder+1)) + spec_thar_ob = np.zeros((2,nord_ob1+nord_ob2,thar_S_ob_B.shape[2])) + + All_Pixel_Centers_R = np.array([]) + All_Wavelengths_R = np.array([]) + All_Orders_R = np.array([]) + All_Centroids_R = np.array([]) + All_Sigmas_R = np.array([]) + All_Intensities_R = np.array([]) + + counter = 0 + temp_pix = np.array([]) + temp_res = np.array([]) + meds,ords = [],[] + for order in range(nord_ob2): order_s = str(order) if (order < 10): order_s = '0' + str(order) thar_order_orig = lines_thar_ob_R[order,:] IV = iv_thar_ob_R[order,:] wei = np.sqrt( IV ) - #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig #- bkg - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms,\ - residuals, centroids,sigmas, intensities = \ - GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+\ - '.iwdat', thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial,\ - FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms,\ + residuals, centroids,sigmas, intensities = \ + GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+\ + '.iwdat', thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial,\ + FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) - if (order == int(np.around(0.5*nord_ob2))): + if (order == int(np.around(0.5*nord_ob2))): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(np.around(0.5*len(thar_order))), len(thar_order) ) else: Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - All_Pixel_Centers_R = np.append( All_Pixel_Centers_R, pixel_centers ) - All_Wavelengths_R = np.append( All_Wavelengths_R, wavelengths ) - All_Orders_R = np.append( All_Orders_R, np.zeros( len(pixel_centers) ) + order ) - All_Centroids_R = np.append( All_Centroids_R, centroids) - All_Sigmas_R = np.append( All_Sigmas_R, sigmas) - All_Intensities_R = np.append( All_Intensities_R, intensities ) - - meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) - ords.append(order) - spec_thar_ob[0,counter] = GLOBALutils.Cheby_eval(coeffs_pix2wav,np.arange(len(thar_order)),len(thar_order)) - spec_thar_ob[1,counter] = thar_order - counter += 1 - pp1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers + sigmas,len(thar_order)) - pm1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers - sigmas,len(thar_order)) - wavsigmas = .5*(pp1 - pm1) - reses = wavelengths/(wavsigmas*2.355) - jji = 0 - vecp,vecm = [],[] - while jji < len(reses): - if jji + 5 < len(reses): - argm = np.argmax(reses[jji:jji+5]) - vecp.append(wavelengths[jji+argm]) - vecm.append(reses[jji+argm]) - jji += 5 - vecp,vecm = np.array(vecp),np.array(vecm) - coef_res = np.polyfit(vecp,vecm,2) - #plot(wavelengths,np.polyval(coef_res,wavelengths)) - #plot(vecp,vecm,'o') - #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) - - c_p2w_ob_R[order] = coeffs_pix2wav - isz = pixel_centers - sigmas - der = pixel_centers + sigmas - isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz,len(thar_order)) - der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der,len(thar_order)) - sig = 0.5*(der-isz) - fwhm = 2.35 * sig - resol = wavelengths / fwhm - temp_pix = np.hstack((temp_pix,pixel_centers)) - temp_res = np.hstack((temp_res,resol)) - #plot(pixel_centers,resol,'o') + All_Pixel_Centers_R = np.append( All_Pixel_Centers_R, pixel_centers ) + All_Wavelengths_R = np.append( All_Wavelengths_R, wavelengths ) + All_Orders_R = np.append( All_Orders_R, np.zeros( len(pixel_centers) ) + order ) + All_Centroids_R = np.append( All_Centroids_R, centroids) + All_Sigmas_R = np.append( All_Sigmas_R, sigmas) + All_Intensities_R = np.append( All_Intensities_R, intensities ) + + meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) + ords.append(order) + spec_thar_ob[0,counter] = GLOBALutils.Cheby_eval(coeffs_pix2wav,np.arange(len(thar_order)),len(thar_order)) + spec_thar_ob[1,counter] = thar_order + counter += 1 + pp1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers + sigmas,len(thar_order)) + pm1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers - sigmas,len(thar_order)) + wavsigmas = .5*(pp1 - pm1) + reses = wavelengths/(wavsigmas*2.355) + jji = 0 + vecp,vecm = [],[] + while jji < len(reses): + if jji + 5 < len(reses): + argm = np.argmax(reses[jji:jji+5]) + vecp.append(wavelengths[jji+argm]) + vecm.append(reses[jji+argm]) + jji += 5 + vecp,vecm = np.array(vecp),np.array(vecm) + coef_res = np.polyfit(vecp,vecm,2) + #plot(wavelengths,np.polyval(coef_res,wavelengths)) + #plot(vecp,vecm,'o') + #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) + + c_p2w_ob_R[order] = coeffs_pix2wav + isz = pixel_centers - sigmas + der = pixel_centers + sigmas + isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz,len(thar_order)) + der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der,len(thar_order)) + sig = 0.5*(der-isz) + fwhm = 2.35 * sig + resol = wavelengths / fwhm + temp_pix = np.hstack((temp_pix,pixel_centers)) + temp_res = np.hstack((temp_res,resol)) + #plot(pixel_centers,resol,'o') p0 = np.zeros( npar_wsol_R ) - p0[0] = (int(np.around(0.5*nord_ob2))+or0_R) * Global_ZP - #GLOBALutils.get_zero_order_number(ords,meds) - p1_R, G_pix_R, G_ord_R, G_wav_R, II_R, rms_ms_R, G_res_R = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_R, All_Wavelengths_R, All_Orders_R,\ - np.ones(All_Intensities_R.shape), p0, Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + p0[0] = (int(np.around(0.5*nord_ob2))+or0_R) * Global_ZP + #GLOBALutils.get_zero_order_number(ords,meds) + p1_R, G_pix_R, G_ord_R, G_wav_R, II_R, rms_ms_R, G_res_R = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_R, All_Wavelengths_R, All_Orders_R,\ + np.ones(All_Intensities_R.shape), p0, Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ order0=or0_R,ntotal=nord_ob2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) - I = np.argsort(temp_pix) - temp_pix,temp_res = temp_pix[I],temp_res[I] - jj = 0 - xx,yy = [],[] - while jj=jj) & (temp_pix0: - xt,yt = temp_pix[I],temp_res[I] - I = np.argmax(yt) - xx.append(xt[I]) - yy.append(yt[I]) - jj+=50 - xx = np.array(xx) - yy = np.array(yy) - coefs_m = np.polyfit(ords,meds,5) - #plot(ords, meds - np.polyval(coefs_m,ords),'ro') - #show() - #plot(xx,yy) - coef = np.polyfit(xx,yy,3) - #print 'Resolution coefs',coef - #plot(np.arange(4096),np.polyval(coef,np.arange(4096)),linewidth=2.0) - #show() - - All_Pixel_Centers_B = np.array([]) - All_Wavelengths_B = np.array([]) - All_Orders_B = np.array([]) - All_Centroids_B = np.array([]) - All_Sigmas_B = np.array([]) - All_Intensities_B = np.array([]) - - meds,ords = [],[] - for order in range(nord_ob1): + I = np.argsort(temp_pix) + temp_pix,temp_res = temp_pix[I],temp_res[I] + jj = 0 + xx,yy = [],[] + while jj=jj) & (temp_pix0: + xt,yt = temp_pix[I],temp_res[I] + I = np.argmax(yt) + xx.append(xt[I]) + yy.append(yt[I]) + jj+=50 + xx = np.array(xx) + yy = np.array(yy) + coefs_m = np.polyfit(ords,meds,5) + #plot(ords, meds - np.polyval(coefs_m,ords),'ro') + #show() + #plot(xx,yy) + coef = np.polyfit(xx,yy,3) + #print 'Resolution coefs',coef + #plot(np.arange(4096),np.polyval(coef,np.arange(4096)),linewidth=2.0) + #show() + + All_Pixel_Centers_B = np.array([]) + All_Wavelengths_B = np.array([]) + All_Orders_B = np.array([]) + All_Centroids_B = np.array([]) + All_Sigmas_B = np.array([]) + All_Intensities_B = np.array([]) + + meds,ords = [],[] + for order in range(nord_ob1): order_s = str(order) if (order < 10): order_s = '0'+str(order) thar_order_orig = lines_thar_ob_B[order,:] IV = iv_thar_ob_B[order,:] wei = np.sqrt( IV ) - #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig #- bkg - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, \ - centroids_B, sigmas, intensities = GLOBALutils.Initial_Wav_Calibration( order_dir\ - +'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial,\ - minlines=minlines_initial, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True,\ - Cheby=use_cheby, porder=porder) + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, \ + centroids_B, sigmas, intensities = GLOBALutils.Initial_Wav_Calibration( order_dir\ + +'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial,\ + minlines=minlines_initial, FixEnds=False,Dump_Argon=dumpargon, Dump_AllLines=True,\ + Cheby=use_cheby, porder=porder) - if (order == int(np.around(0.5*nord_ob1))): + if (order == int(np.around(0.5*nord_ob1))): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(np.around(0.5*len(thar_order))), len(thar_order) ) else: Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - All_Pixel_Centers_B = np.append( All_Pixel_Centers_B, pixel_centers ) - All_Wavelengths_B = np.append( All_Wavelengths_B, wavelengths ) - All_Orders_B = np.append( All_Orders_B, np.zeros( len(pixel_centers) ) + order ) - All_Centroids_B = np.append( All_Centroids_B, centroids) - All_Sigmas_B = np.append( All_Sigmas_B, sigmas) - All_Intensities_B = np.append( All_Intensities_B, intensities ) - - meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) - ords.append(order) - spec_thar_ob[0,counter] = GLOBALutils.Cheby_eval(coeffs_pix2wav,np.arange(len(thar_order)),len(thar_order)) - spec_thar_ob[1,counter] = thar_order - counter += 1 - pp1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers + sigmas,len(thar_order)) - pm1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers - sigmas,len(thar_order)) - wavsigmas = .5*(pp1 - pm1) - reses = wavelengths/(wavsigmas*2.355) - jji = 0 - vecp,vecm = [],[] - while jji < len(reses): - if jji + 5 < len(reses): - argm = np.argmax(reses[jji:jji+5]) - vecp.append(wavelengths[jji+argm]) - vecm.append(reses[jji+argm]) - jji += 5 - vecp,vecm = np.array(vecp),np.array(vecm) - coef_res = np.polyfit(vecp,vecm,2) - #plot(wavelengths,np.polyval(coef_res,wavelengths)) - #plot(vecp,vecm,'o') - c_p2w_ob_B[order] = coeffs_pix2wav - #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) - + All_Pixel_Centers_B = np.append( All_Pixel_Centers_B, pixel_centers ) + All_Wavelengths_B = np.append( All_Wavelengths_B, wavelengths ) + All_Orders_B = np.append( All_Orders_B, np.zeros( len(pixel_centers) ) + order ) + All_Centroids_B = np.append( All_Centroids_B, centroids) + All_Sigmas_B = np.append( All_Sigmas_B, sigmas) + All_Intensities_B = np.append( All_Intensities_B, intensities ) + + meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) + ords.append(order) + spec_thar_ob[0,counter] = GLOBALutils.Cheby_eval(coeffs_pix2wav,np.arange(len(thar_order)),len(thar_order)) + spec_thar_ob[1,counter] = thar_order + counter += 1 + pp1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers + sigmas,len(thar_order)) + pm1 = GLOBALutils.Cheby_eval(coeffs_pix2wav,pixel_centers - sigmas,len(thar_order)) + wavsigmas = .5*(pp1 - pm1) + reses = wavelengths/(wavsigmas*2.355) + jji = 0 + vecp,vecm = [],[] + while jji < len(reses): + if jji + 5 < len(reses): + argm = np.argmax(reses[jji:jji+5]) + vecp.append(wavelengths[jji+argm]) + vecm.append(reses[jji+argm]) + jji += 5 + vecp,vecm = np.array(vecp),np.array(vecm) + coef_res = np.polyfit(vecp,vecm,2) + #plot(wavelengths,np.polyval(coef_res,wavelengths)) + #plot(vecp,vecm,'o') + c_p2w_ob_B[order] = coeffs_pix2wav + #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) + p0 = np.zeros( npar_wsol_B ) - p0[0] = (int(np.around(0.5*nord_ob1))+or0_B) * Global_ZP - #GLOBALutils.get_zero_order_number(ords,meds) - p1_B, G_pix_B, G_ord_B, G_wav_B, II_B, rms_ms_B, G_res_B = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_B, All_Wavelengths_B, All_Orders_B,\ - np.ones(All_Intensities_B.shape), p0, Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + p0[0] = (int(np.around(0.5*nord_ob1))+or0_B) * Global_ZP + #GLOBALutils.get_zero_order_number(ords,meds) + p1_B, G_pix_B, G_ord_B, G_wav_B, II_B, rms_ms_B, G_res_B = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_B, All_Wavelengths_B, All_Orders_B,\ + np.ones(All_Intensities_B.shape), p0, Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ order0=or0_B,ntotal=nord_ob1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) - nhdu = pyfits.PrimaryHDU(spec_thar_ob) - if os.access(wavsol_fits,os.F_OK): - os.system('rm '+wavsol_fits) - nhdu.writeto(wavsol_fits) - - #plot(ords,meds,'ro') - #coefs_m = np.polyfit(ords,meds,6) - #plot(ords, meds - np.polyval(coefs_m,ords),'ro') - #show() - All_Pixel_Centers_co_R = np.array([]) - All_Wavelengths_co_R = np.array([]) - All_Orders_co_R = np.array([]) - All_Centroids_co_R = np.array([]) - All_Sigmas_co_R = np.array([]) - All_Intensities_co_R = np.array([]) - meds,ords = [],[] + nhdu = pyfits.PrimaryHDU(spec_thar_ob) + if os.access(wavsol_fits,os.F_OK): + os.system('rm '+wavsol_fits) + nhdu.writeto(wavsol_fits) + + #plot(ords,meds,'ro') + #coefs_m = np.polyfit(ords,meds,6) + #plot(ords, meds - np.polyval(coefs_m,ords),'ro') + #show() + All_Pixel_Centers_co_R = np.array([]) + All_Wavelengths_co_R = np.array([]) + All_Orders_co_R = np.array([]) + All_Centroids_co_R = np.array([]) + All_Sigmas_co_R = np.array([]) + All_Intensities_co_R = np.array([]) + meds,ords = [],[] for order in range(nord_co2): order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar_co_R[order,:] IV = iv_thar_co_R[order,:] wei = np.sqrt( IV ) - #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig #- bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial,\ - FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) + = GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial,\ + FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) - c_p2w_co_R[order] = coeffs_pix2wav - meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) - ords.append(order) - if (order == int(np.around(0.5*nord_co2))): + c_p2w_co_R[order] = coeffs_pix2wav + meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) + ords.append(order) + if (order == int(np.around(0.5*nord_co2))): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(np.around(0.5*len(thar_order))), len(thar_order) ) else: Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - All_Pixel_Centers_co_R = np.append( All_Pixel_Centers_co_R, pixel_centers ) - All_Wavelengths_co_R = np.append( All_Wavelengths_co_R, wavelengths ) - All_Orders_co_R = np.append( All_Orders_co_R, np.zeros( len(pixel_centers) ) + order ) - All_Centroids_co_R = np.append( All_Centroids_co_R, centroids) - All_Sigmas_co_R = np.append( All_Sigmas_co_R, sigmas) - All_Intensities_co_R = np.append( All_Intensities_co_R, intensities ) - - #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) + All_Pixel_Centers_co_R = np.append( All_Pixel_Centers_co_R, pixel_centers ) + All_Wavelengths_co_R = np.append( All_Wavelengths_co_R, wavelengths ) + All_Orders_co_R = np.append( All_Orders_co_R, np.zeros( len(pixel_centers) ) + order ) + All_Centroids_co_R = np.append( All_Centroids_co_R, centroids) + All_Sigmas_co_R = np.append( All_Sigmas_co_R, sigmas) + All_Intensities_co_R = np.append( All_Intensities_co_R, intensities ) + + #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) p0 = np.zeros( npar_wsol_R ) - p0[0] = (int(np.around(0.5*nord_co2))+or0_R) * Global_ZP - #GLOBALutils.get_zero_order_number(ords,meds) - p1_co_R, G_pix_co_R, G_ord_co_R, G_wav_co_R, II_co_R, rms_ms_co_R, G_res_co_R = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co_R, All_Wavelengths_co_R, All_Orders_co_R,\ - np.ones(All_Intensities_co_R.shape), p0, Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + p0[0] = (int(np.around(0.5*nord_co2))+or0_R) * Global_ZP + #GLOBALutils.get_zero_order_number(ords,meds) + p1_co_R, G_pix_co_R, G_ord_co_R, G_wav_co_R, II_co_R, rms_ms_co_R, G_res_co_R = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co_R, All_Wavelengths_co_R, All_Orders_co_R,\ + np.ones(All_Intensities_co_R.shape), p0, Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ order0=or0_R,ntotal=nord_co2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) - All_Pixel_Centers_co_B = np.array([]) - All_Wavelengths_co_B = np.array([]) - All_Orders_co_B = np.array([]) - All_Centroids_co_B = np.array([]) - All_Sigmas_co_B = np.array([]) - All_Intensities_co_B = np.array([]) - meds,ords = [],[] + All_Pixel_Centers_co_B = np.array([]) + All_Wavelengths_co_B = np.array([]) + All_Orders_co_B = np.array([]) + All_Centroids_co_B = np.array([]) + All_Sigmas_co_B = np.array([]) + All_Intensities_co_B = np.array([]) + meds,ords = [],[] for order in range(nord_co1): - order = order + 1 + order = order + 1 order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar_co_B[order-1,:] IV = iv_thar_co_B[order-1,:] wei = np.sqrt( IV ) - #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig #- bkg coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals,\ - centroids, sigmas, intensities = GLOBALutils.Initial_Wav_Calibration( order_dir+\ - 'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, \ - minlines=minlines_initial, FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True,\ - Cheby=use_cheby, porder=porder) - c_p2w_co_B[order-1] = coeffs_pix2wav - - meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) - ords.append(order) - if (order == int(np.around(0.5*nord_co1))): + centroids, sigmas, intensities = GLOBALutils.Initial_Wav_Calibration( order_dir+\ + 'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, \ + minlines=minlines_initial, FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True,\ + Cheby=use_cheby, porder=porder) + c_p2w_co_B[order-1] = coeffs_pix2wav + + meds.append(GLOBALutils.Cheby_eval(coeffs_pix2wav,0.5*len(thar_order),len(thar_order))) + ords.append(order) + if (order == int(np.around(0.5*nord_co1))): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, int(np.around(0.5*len(thar_order))), len(thar_order) ) else: Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - All_Pixel_Centers_co_B = np.append( All_Pixel_Centers_co_B, pixel_centers ) - All_Wavelengths_co_B = np.append( All_Wavelengths_co_B, wavelengths ) - All_Orders_co_B = np.append( All_Orders_co_B, np.zeros( len(pixel_centers) ) + order ) - All_Centroids_co_B = np.append( All_Centroids_co_B, centroids) - All_Sigmas_co_B = np.append( All_Sigmas_co_B, sigmas) - All_Intensities_co_B = np.append( All_Intensities_co_B, intensities ) + All_Pixel_Centers_co_B = np.append( All_Pixel_Centers_co_B, pixel_centers ) + All_Wavelengths_co_B = np.append( All_Wavelengths_co_B, wavelengths ) + All_Orders_co_B = np.append( All_Orders_co_B, np.zeros( len(pixel_centers) ) + order ) + All_Centroids_co_B = np.append( All_Centroids_co_B, centroids) + All_Sigmas_co_B = np.append( All_Sigmas_co_B, sigmas) + All_Intensities_co_B = np.append( All_Intensities_co_B, intensities ) - #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) + #print order, rms_ms/np.sqrt(float(len(wavelengths))), rms_ms, len(residuals) p0 = np.zeros( npar_wsol_B ) - p0[0] = (int(np.around(0.5*nord_co1))+or0_B) * Global_ZP - #GLOBALutils.get_zero_order_number(ords,meds) - p1_co_B, G_pix_co_B, G_ord_co_B, G_wav_co_B, II_co_B, rms_ms_co_B, G_res_co_B = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co_B, All_Wavelengths_co_B, All_Orders_co_B,\ - np.ones(All_Intensities_co_B.shape), p0, Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + p0[0] = (int(np.around(0.5*nord_co1))+or0_B) * Global_ZP + #GLOBALutils.get_zero_order_number(ords,meds) + p1_co_B, G_pix_co_B, G_ord_co_B, G_wav_co_B, II_co_B, rms_ms_co_B, G_res_co_B = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers_co_B, All_Wavelengths_co_B, All_Orders_co_B,\ + np.ones(All_Intensities_co_B.shape), p0, Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ order0=or0_B,ntotal=nord_co1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) - #pdict = {'p2w_ob_R':c_p2w_ob_R, 'p2w_ob_B':c_p2w_ob_B, 'p2w_co_R':c_p2w_co_R, 'p2w_co_B':c_p2w_co_B} + #pdict = {'p2w_ob_R':c_p2w_ob_R, 'p2w_ob_B':c_p2w_ob_B, 'p2w_co_R':c_p2w_co_R, 'p2w_co_B':c_p2w_co_B} pdict = {'p1_R':p1_R,'p1_B':p1_B,'p1_co_R':p1_co_R,'p1_co_B':p1_co_B,\ - 'G_pix_R':G_pix_R, 'G_pix_B':G_pix_B,'G_pix_co_R':G_pix_co_R, 'G_pix_co_B':G_pix_co_B,\ - 'G_ord_R':G_ord_R, 'G_ord_B':G_ord_B,'G_ord_co_R':G_ord_co_R, 'G_ord_co_B':G_ord_co_B,\ - 'G_wav_R':G_wav_R, 'G_wav_B':G_wav_B,'G_wav_co_R':G_wav_co_R, 'G_wav_co_B':G_wav_co_B,\ - 'II_R':II_R,'II_B':II_B,'II_co_R':II_co_R,'II_co_B':II_co_B,\ - 'rms_ms_R':rms_ms_R,'rms_ms_B':rms_ms_B,'rms_ms_co_R':rms_ms_co_R,'rms_ms_co_B':rms_ms_co_B,\ - 'G_res_R':G_res_R, 'G_res_B':G_res_B,'G_res_co_R':G_res_co_R, 'G_res_co_B':G_res_co_B,\ - 'All_Centroids_R':All_Centroids_R,'All_Centroids_B':All_Centroids_B,\ - 'All_Centroids_co_R':All_Centroids_co_R,'All_Centroids_co_B':All_Centroids_co_B,\ - 'All_Orders_R':All_Orders_R,'All_Orders_B':All_Orders_B,\ - 'All_Orders_co_R':All_Orders_co_R,'All_Orders_co_B':All_Orders_co_B,\ - 'All_Sigmas_R':All_Sigmas_R,'All_Sigmas_B':All_Sigmas_B,\ - 'All_Sigmas_co_R':All_Sigmas_co_R,'All_Sigmas_co_B':All_Sigmas_co_B,\ - 'mjd':mjd,'npix':len(thar_order)} + 'G_pix_R':G_pix_R, 'G_pix_B':G_pix_B,'G_pix_co_R':G_pix_co_R, 'G_pix_co_B':G_pix_co_B,\ + 'G_ord_R':G_ord_R, 'G_ord_B':G_ord_B,'G_ord_co_R':G_ord_co_R, 'G_ord_co_B':G_ord_co_B,\ + 'G_wav_R':G_wav_R, 'G_wav_B':G_wav_B,'G_wav_co_R':G_wav_co_R, 'G_wav_co_B':G_wav_co_B,\ + 'II_R':II_R,'II_B':II_B,'II_co_R':II_co_R,'II_co_B':II_co_B,\ + 'rms_ms_R':rms_ms_R,'rms_ms_B':rms_ms_B,'rms_ms_co_R':rms_ms_co_R,'rms_ms_co_B':rms_ms_co_B,\ + 'G_res_R':G_res_R, 'G_res_B':G_res_B,'G_res_co_R':G_res_co_R, 'G_res_co_B':G_res_co_B,\ + 'All_Centroids_R':All_Centroids_R,'All_Centroids_B':All_Centroids_B,\ + 'All_Centroids_co_R':All_Centroids_co_R,'All_Centroids_co_B':All_Centroids_co_B,\ + 'All_Orders_R':All_Orders_R,'All_Orders_B':All_Orders_B,\ + 'All_Orders_co_R':All_Orders_co_R,'All_Orders_co_B':All_Orders_co_B,\ + 'All_Sigmas_R':All_Sigmas_R,'All_Sigmas_B':All_Sigmas_B,\ + 'All_Sigmas_co_R':All_Sigmas_co_R,'All_Sigmas_co_B':All_Sigmas_co_B,\ + 'mjd':mjd,'npix':len(thar_order)} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) xc_fout_f = dirout+'ThAr_XCor+DeltaL.dat' -if ( (os.access(xc_fout_f,os.F_OK) == False) or (force_tharxc)): +if ( (os.access(xc_fout_f,os.F_OK) == False) or (force_tharxc)): xc_fout = open(xc_fout_f,'w') for i in range(len(sorted_ThAr_dates)): index = sorted_ThAr_dates[i] @@ -921,45 +922,45 @@ fsim = ThAr_ref[index] hthar = pyfits.open( fsim ) mjd, mjd0 = harpsutils.mjd_fromheader( hthar ) - pdict1 = pickle.load( open(dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl','r' ) ) + pdict1 = pickle.load( open(dirout + fsim.split('/')[-1][:-4]+'wavsolpars.pkl','r' ) ) for ii in range(len(sorted_ThAr_dates)): - index2 = sorted_ThAr_dates[ii] + index2 = sorted_ThAr_dates[ii] fsim2 = ThAr_ref[index2] hthar2 = pyfits.open( fsim2 ) mjd2, mjd02 = harpsutils.mjd_fromheader( hthar2 ) - pdict2 = pickle.load( open(dirout + fsim2.split('/')[-1][:-4]+'wavsolpars.pkl','r' ) ) - - p_shift_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_R'], pdict2['G_wav_R'], pdict2['G_ord_R'],\ - np.ones(pdict2['G_wav_R'].shape), pdict1['p1_R'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_R,ntotal=nord_ob2,npix=pdict2['npix'],nx=ncoef_x_R,nm=ncoef_m_R) - - p_shift_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_B'], pdict2['G_wav_B'], pdict2['G_ord_B'],\ - np.ones(pdict2['G_wav_B'].shape), pdict1['p1_B'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_B,ntotal=nord_ob1,npix=pdict2['npix'],nx=ncoef_x_B,nm=ncoef_m_B) - - p_shift_co_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_co_R'], pdict2['G_wav_co_R'], pdict2['G_ord_co_R'],\ - np.ones(pdict2['G_wav_co_R'].shape), pdict1['p1_co_R'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_R,ntotal=nord_co2,npix=pdict2['npix'],nx=ncoef_x_R,nm=ncoef_m_R) - - p_shift_co_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_co_B'], pdict2['G_wav_co_B'], pdict2['G_ord_co_B'],\ - np.ones(pdict2['G_wav_co_B'].shape), pdict1['p1_co_B'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_B,ntotal=nord_co1,npix=pdict2['npix'],nx=ncoef_x_B,nm=ncoef_m_B) - - - # write out products - line_out = "%20.12f %20.12f %12.6f %12.6f %12.6f %12.6f %s %s\n" % (mjd, mjd2,(1e-6*p_shift_R)*299792458.0,(1e-6*p_shift_B)*299792458.0,(1e-6*p_shift_co_R)*299792458.0,(1e-6*p_shift_co_B)*299792458.0,fsim.split('/')[-1][:-4],fsim2.split('/')[-1][:-4]) - xc_fout.write(line_out) - #xc_fout.flush() + pdict2 = pickle.load( open(dirout + fsim2.split('/')[-1][:-4]+'wavsolpars.pkl','r' ) ) + + p_shift_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_R'], pdict2['G_wav_R'], pdict2['G_ord_R'],\ + np.ones(pdict2['G_wav_R'].shape), pdict1['p1_R'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_R,ntotal=nord_ob2,npix=pdict2['npix'],nx=ncoef_x_R,nm=ncoef_m_R) + + p_shift_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_B'], pdict2['G_wav_B'], pdict2['G_ord_B'],\ + np.ones(pdict2['G_wav_B'].shape), pdict1['p1_B'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_B,ntotal=nord_ob1,npix=pdict2['npix'],nx=ncoef_x_B,nm=ncoef_m_B) + + p_shift_co_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_co_R'], pdict2['G_wav_co_R'], pdict2['G_ord_co_R'],\ + np.ones(pdict2['G_wav_co_R'].shape), pdict1['p1_co_R'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_R,ntotal=nord_co2,npix=pdict2['npix'],nx=ncoef_x_R,nm=ncoef_m_R) + + p_shift_co_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pdict2['G_pix_co_B'], pdict2['G_wav_co_B'], pdict2['G_ord_co_B'],\ + np.ones(pdict2['G_wav_co_B'].shape), pdict1['p1_co_B'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_B,ntotal=nord_co1,npix=pdict2['npix'],nx=ncoef_x_B,nm=ncoef_m_B) + + + # write out products + line_out = "%20.12f %20.12f %12.6f %12.6f %12.6f %12.6f %s %s\n" % (mjd, mjd2,(1e-6*p_shift_R)*299792458.0,(1e-6*p_shift_B)*299792458.0,(1e-6*p_shift_co_R)*299792458.0,(1e-6*p_shift_co_B)*299792458.0,fsim.split('/')[-1][:-4],fsim2.split('/')[-1][:-4]) + xc_fout.write(line_out) + #xc_fout.flush() xc_fout.close() @@ -972,26 +973,26 @@ fsim = sim_sci[i] obname = obnames[i] texp = exptimes[i] - co_type = co_types[i] + co_type = co_types[i] if (object2do == 'all'): new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) - new_list_cotypes.append( co_type ) + new_list_cotypes.append( co_type ) elif object2do == 'new': - h = pyfits.open(fsim) - fout = 'proc/'+ obname + '_' + h[0].header['DATE-OBS'] + 'sp.fits' + h = pyfits.open(fsim) + fout = 'proc/'+ obname + '_' + h[0].header['DATE-OBS'] + 'sp.fits' if not os.access(dirout + fout,os.F_OK): new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) - new_list_cotypes.append( co_type ) + new_list_cotypes.append( co_type ) else: if (obname == object2do): new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) - new_list_cotypes.append( co_type ) + new_list_cotypes.append( co_type ) if os.access(dirin + 'moon_corr.txt', os.F_OK): fmoon = open(dirin + 'moon_corr.txt','r') @@ -1010,9 +1011,9 @@ spec_moon = np.array(spec_moon) use_moon = np.array(use_moon) -print '\n\tThe following targets will be processed:\n' +print('\n\tThe following targets will be processed:\n') for nlisti in range(len(new_list)): - print '\t'+new_list_obnames[nlisti] + print('\t'+new_list_obnames[nlisti]) for nlisti in range(len(new_list)): fsim = new_list[ nlisti ] @@ -1028,9 +1029,9 @@ here_moon = use_moon[I] h = pyfits.open(fsim) - print '\n' - print "\t--> Working on image: ", fsim - print "\t\tObject name:",obname + print('\n') + print("\t--> Working on image: ", fsim) + print("\t\tObject name:",obname) mjd,mjd0 = harpsutils.mjd_fromheader(h) # Open file, trim, overscan subtract and MasterBias subtract @@ -1040,16 +1041,16 @@ dataR = h[2].data dataR = harpsutils.OverscanTrim(dataR) dataR -= MasterBias[:,:,1] - + if cotype != 'WAVE': - spanR,spanB = 20,10 - c1_temp = c_ob1 - c2_temp = c_ob2 + spanR,spanB = 20,10 + c1_temp = c_ob1 + c2_temp = c_ob2 else: - spanR,spanB = 10,7 - c1_temp = c_all1 - c2_temp = c_all2 - + spanR,spanB = 10,7 + c1_temp = c_all1 + c2_temp = c_all2 + bacfile = dirout + 'BACR_' + fsim.split('/')[-1][:-4]+'fits' if os.access(bacfile,os.F_OK) == False: CentersR = np.zeros((len(c2_temp),dataR.shape[1])) @@ -1068,8 +1069,8 @@ CentersB = np.zeros((len(c1_temp),dataB.shape[1])) for i in range(len(c1_temp)): CentersB[i,:]=np.polyval(c1_temp[i],np.arange(dataB.shape[1])) - tcen = CentersB[-1] - CentersB = np.vstack((CentersB,tcen+16.)) + tcen = CentersB[-1] + CentersB = np.vstack((CentersB,tcen+16.)) bacB = GLOBALutils.get_scat(dataB,CentersB,span=10) if (os.access(bacfile,os.F_OK)): os.remove( bacfile ) @@ -1101,10 +1102,10 @@ ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 + ra = ra2 + dec = dec2 else: - print 'Using the coordinates found in the image header.' + print('Using the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -1116,15 +1117,15 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "Barycentric velocity:", bcvel_baryc, mjd + print("Barycentric velocity:", bcvel_baryc, mjd) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name='Eso3.6' - gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs = ephem.Observer() + gobs.name='Eso3.6' + gobs.lat = rad(latitude) # lat/long in decimal degrees gobs.long = rad(longitude) DDATE = h[0].header['DATE-OBS'][:10] HHOUR = h[0].header['DATE-OBS'][11:] @@ -1140,7 +1141,7 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sorted_indices = np.argsort( np.abs( np.array(ThAr_ref_dates) - mjd ) ) @@ -1154,7 +1155,7 @@ or ( os.access(sci_fits_ob_R,os.F_OK) == False ) or ( os.access(sci_fits_co_R,os.F_OK) == False ) \ or (force_sci_extract): - print "No previous extraction or extraction forced for science file", fsim, "extracting..." + print("No previous extraction or extraction forced for science file", fsim, "extracting...") sci_S_ob_B = np.zeros( (nord_ob1,3,dataB.shape[1]) ) sci_S_co_B = np.zeros( (nord_co1,3,dataB.shape[1]) ) sci_S_ob_R = np.zeros( (nord_ob2,3,dataR.shape[1]) ) @@ -1165,7 +1166,7 @@ ron1,gain1,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - sci_S_co_B = GLOBALutils.optimal_extraction(dataB,P_co_B,c_co1,ext_aperture_B,\ + sci_S_co_B = GLOBALutils.optimal_extraction(dataB,P_co_B,c_co1,ext_aperture_B,\ ron1,gain1,S_Marsh,100.*NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) @@ -1173,14 +1174,14 @@ ron2,gain2,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - sci_S_co_R = GLOBALutils.optimal_extraction(dataR,P_co_R,c_co2,ext_aperture_R,\ + sci_S_co_R = GLOBALutils.optimal_extraction(dataR,P_co_R,c_co2,ext_aperture_R,\ ron2,gain2,S_Marsh,100.*NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - sci_S_ob_B = GLOBALutils.invert(sci_S_ob_B) - sci_S_co_B = GLOBALutils.invert(sci_S_co_B) - sci_S_ob_R = GLOBALutils.invert(sci_S_ob_R) - sci_S_co_R = GLOBALutils.invert(sci_S_co_R) + sci_S_ob_B = GLOBALutils.invert(sci_S_ob_B) + sci_S_co_B = GLOBALutils.invert(sci_S_co_B) + sci_S_ob_R = GLOBALutils.invert(sci_S_ob_R) + sci_S_co_R = GLOBALutils.invert(sci_S_co_R) sci_S_ob_B,sci_S_co_B,sci_S_ob_R,sci_S_co_R = sci_S_ob_B[::-1],sci_S_co_B[::-1],sci_S_ob_R[::-1],sci_S_co_R[::-1] # save as fits file @@ -1192,7 +1193,7 @@ os.remove( sci_fits_ob_R ) if (os.access(sci_fits_co_R,os.F_OK)): os.remove( sci_fits_co_R ) - + hdu = pyfits.PrimaryHDU( sci_S_ob_B ) hdu.writeto( sci_fits_ob_B ) hdu = pyfits.PrimaryHDU( sci_S_co_B ) @@ -1203,7 +1204,7 @@ hdu.writeto( sci_fits_co_R ) else: - print fsim, "has already been extracted, reading in product fits files..." + print(fsim, "has already been extracted, reading in product fits files...") sci_S_ob_B = pyfits.getdata( sci_fits_ob_B ) sci_S_co_B = pyfits.getdata( sci_fits_co_B ) sci_S_ob_R = pyfits.getdata( sci_fits_ob_R ) @@ -1213,7 +1214,7 @@ dateobs = h[0].header['DATE-OBS'][:4] + h[0].header['DATE-OBS'][5:7] + h[0].header['DATE-OBS'][8:9] #Build spectra - + if ( os.access(dirout+fout ,os.F_OK) == False ) or (force_spectral_file_build): # initialize file that will have the spectra spec = np.zeros((11, nord_ob2 + nord_ob1, dataB.shape[1])) @@ -1225,7 +1226,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH FLUX WEIGHTED MEAN F ',h[0].header['HIERARCH ESO INS DET1 TMMEAN']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -1237,8 +1238,8 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',h[0].header['HIERARCH ESO TEL GEOELEV']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',h[0].header['HIERARCH ESO TEL AIRM START']) - print '\t\tWavelength calibration:' - print '\t\t\tComparision fibre is '+ cotype + print('\t\tWavelength calibration:') + print('\t\t\tComparision fibre is '+ cotype) indice = sorted_indices[0] thar_fits_ob_R = dirout + ThAr_ref[indice].split('/')[-1][:-4]+'spec.ob.R.fits.S' @@ -1246,125 +1247,125 @@ thar_fits_ob_B = dirout + ThAr_ref[indice].split('/')[-1][:-4]+'spec.ob.B.fits.S' thar_fits_co_B = dirout + ThAr_ref[indice].split('/')[-1][:-4]+'spec.co.B.fits.S' pkl_wsol = dirout + ThAr_ref[indice].split('/')[-1][:-4]+'wavsolpars.pkl' - print "\t\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + print("\t\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) - cotype = 'SKY' - if cotype == 'WAVE': - # Extract thAr lines from comparison orders - lines_thar_co_R = sci_S_co_R[:,1,:] - iv_thar_co_R = sci_S_co_R[:,2,:] - lines_thar_co_B = sci_S_co_B[:,1,:] - iv_thar_co_B = sci_S_co_B[:,2,:] - - Red_Pixel_Centers_co = np.array([]) - Red_Wavelengths_co = np.array([]) - Red_Orders_co = np.array([]) - Red_Centroids_co = np.array([]) - Red_Sigmas_co = np.array([]) - Red_Intensities_co = np.array([]) - - for order in range(nord_co2): - order_s = str(order) - if (order < 10): - order_s = '0'+str(order) - - thar_order_orig = lines_thar_co_R[order,:] - IV = iv_thar_co_R[order,:] - wei = np.sqrt( IV ) - #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) - thar_order = thar_order_orig #- bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial, \ - FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) - - Red_Pixel_Centers_co = np.append( Red_Pixel_Centers_co, pixel_centers ) - Red_Wavelengths_co = np.append( Red_Wavelengths_co, wavelengths ) - Red_Orders_co = np.append( Red_Orders_co, np.zeros( len(pixel_centers) ) + order ) - Red_Centroids_co = np.append( Red_Centroids_co, centroids) - Red_Sigmas_co = np.append( Red_Sigmas_co, sigmas) - Red_Intensities_co = np.append( Red_Intensities_co, intensities ) - - p1_co_R, G_pix_co_R, G_ord_co_R, G_wav_co_R, II_co_R, rms_ms_co_R, G_res_co_R = \ - GLOBALutils.Fit_Global_Wav_Solution(Red_Pixel_Centers_co, Red_Wavelengths_co, Red_Orders_co,\ - np.ones(Red_Intensities_co.shape), wsol_dict['p1_co_R'], Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ - order0=or0_R,ntotal=nord_co2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) - - p_shift_co_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co_R, G_wav_co_R, G_ord_co_R,\ - np.ones(G_wav_co_R.shape), wsol_dict['p1_co_R'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_R,ntotal=nord_co2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) - - weight_R = (np.sqrt(len(orders)) / rms_ms)**2 - - - Blue_Pixel_Centers_co = np.array([]) - Blue_Wavelengths_co = np.array([]) - Blue_Orders_co = np.array([]) - Blue_Centroids_co = np.array([]) - Blue_Sigmas_co = np.array([]) - Blue_Intensities_co = np.array([]) - - for order in range(nord_co1): - order = order + 1 - order_s = str(order) - if (order < 10): - order_s = '0'+str(order) - - thar_order_orig = lines_thar_co_B[order-1,:] - IV = iv_thar_co_B[order-1,:] - wei = np.sqrt( IV ) - thar_order = thar_order_orig #- bkg - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration( order_dir+'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=50, \ - FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) - - Blue_Pixel_Centers_co = np.append( Blue_Pixel_Centers_co, pixel_centers ) - Blue_Wavelengths_co = np.append( Blue_Wavelengths_co, wavelengths ) - Blue_Orders_co = np.append( Blue_Orders_co, np.zeros( len(pixel_centers) ) + order ) - Blue_Centroids_co = np.append( Blue_Centroids_co, centroids) - Blue_Sigmas_co = np.append( Blue_Sigmas_co, sigmas) - Blue_Intensities_co = np.append( Blue_Intensities_co, intensities ) - - p1_co_B, G_pix_co_B, G_ord_co_B, G_wav_co_B, II_co_B, rms_ms_co_B, G_res_co_B = \ - GLOBALutils.Fit_Global_Wav_Solution(Blue_Pixel_Centers_co, Blue_Wavelengths_co, Blue_Orders_co,\ - np.ones(Blue_Intensities_co.shape), wsol_dict['p1_co_B'], Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ - order0=or0_B,ntotal=nord_co1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) + cotype = 'SKY' + if cotype == 'WAVE': + # Extract thAr lines from comparison orders + lines_thar_co_R = sci_S_co_R[:,1,:] + iv_thar_co_R = sci_S_co_R[:,2,:] + lines_thar_co_B = sci_S_co_B[:,1,:] + iv_thar_co_B = sci_S_co_B[:,2,:] + + Red_Pixel_Centers_co = np.array([]) + Red_Wavelengths_co = np.array([]) + Red_Orders_co = np.array([]) + Red_Centroids_co = np.array([]) + Red_Sigmas_co = np.array([]) + Red_Intensities_co = np.array([]) + + for order in range(nord_co2): + order_s = str(order) + if (order < 10): + order_s = '0'+str(order) + + thar_order_orig = lines_thar_co_R[order,:] + IV = iv_thar_co_R[order,:] + wei = np.sqrt( IV ) + #bkg = CoralieUtils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + thar_order = thar_order_orig #- bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'R_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=minlines_initial, \ + FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) + + Red_Pixel_Centers_co = np.append( Red_Pixel_Centers_co, pixel_centers ) + Red_Wavelengths_co = np.append( Red_Wavelengths_co, wavelengths ) + Red_Orders_co = np.append( Red_Orders_co, np.zeros( len(pixel_centers) ) + order ) + Red_Centroids_co = np.append( Red_Centroids_co, centroids) + Red_Sigmas_co = np.append( Red_Sigmas_co, sigmas) + Red_Intensities_co = np.append( Red_Intensities_co, intensities ) + + p1_co_R, G_pix_co_R, G_ord_co_R, G_wav_co_R, II_co_R, rms_ms_co_R, G_res_co_R = \ + GLOBALutils.Fit_Global_Wav_Solution(Red_Pixel_Centers_co, Red_Wavelengths_co, Red_Orders_co,\ + np.ones(Red_Intensities_co.shape), wsol_dict['p1_co_R'], Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + order0=or0_R,ntotal=nord_co2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) + + p_shift_co_R, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co_R, G_wav_co_R, G_ord_co_R,\ + np.ones(G_wav_co_R.shape), wsol_dict['p1_co_R'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_R,ntotal=nord_co2,npix=len(thar_order),nx=ncoef_x_R,nm=ncoef_m_R) + + weight_R = (np.sqrt(len(orders)) / rms_ms)**2 + + + Blue_Pixel_Centers_co = np.array([]) + Blue_Wavelengths_co = np.array([]) + Blue_Orders_co = np.array([]) + Blue_Centroids_co = np.array([]) + Blue_Sigmas_co = np.array([]) + Blue_Intensities_co = np.array([]) + + for order in range(nord_co1): + order = order + 1 + order_s = str(order) + if (order < 10): + order_s = '0'+str(order) + + thar_order_orig = lines_thar_co_B[order-1,:] + IV = iv_thar_co_B[order-1,:] + wei = np.sqrt( IV ) + thar_order = thar_order_orig #- bkg + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths, rms_ms, residuals, centroids, sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration( order_dir+'B_order_'+order_s+final_wav, thar_order, order, wei, rmsmax=MRMS_initial, minlines=50, \ + FixEnds=True,Dump_Argon=dumpargon, Dump_AllLines=True, Cheby=use_cheby, porder=porder) + + Blue_Pixel_Centers_co = np.append( Blue_Pixel_Centers_co, pixel_centers ) + Blue_Wavelengths_co = np.append( Blue_Wavelengths_co, wavelengths ) + Blue_Orders_co = np.append( Blue_Orders_co, np.zeros( len(pixel_centers) ) + order ) + Blue_Centroids_co = np.append( Blue_Centroids_co, centroids) + Blue_Sigmas_co = np.append( Blue_Sigmas_co, sigmas) + Blue_Intensities_co = np.append( Blue_Intensities_co, intensities ) + + p1_co_B, G_pix_co_B, G_ord_co_B, G_wav_co_B, II_co_B, rms_ms_co_B, G_res_co_B = \ + GLOBALutils.Fit_Global_Wav_Solution(Blue_Pixel_Centers_co, Blue_Wavelengths_co, Blue_Orders_co,\ + np.ones(Blue_Intensities_co.shape), wsol_dict['p1_co_B'], Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m, minlines=minlines_glob,\ + order0=or0_B,ntotal=nord_co1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) + + p_shift_co_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co_B, G_wav_co_B, G_ord_co_B,\ + np.ones(G_wav_co_B.shape), wsol_dict['p1_co_B'],\ + Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ + order0=or0_B,ntotal=nord_co1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) + weight_B = (np.sqrt(len(orders)) / rms_ms)**2 + + shift = (p_shift_co_R[0]*weight_R + p_shift_co_B[0]*weight_B) / weight_R + weight_B + print(p_shift_co_R,p_shift_co_B,shift) + else: + p_shift_co_R = [0.] + p_shift_co_B = [0.] + p_shift = 0. + shift = 0. - p_shift_co_B, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix_co_B, G_wav_co_B, G_ord_co_B,\ - np.ones(G_wav_co_B.shape), wsol_dict['p1_co_B'],\ - Cheby=True,Inv=True,maxrms=MRMS,minlines=minlines_glob,\ - order0=or0_B,ntotal=nord_co1,npix=len(thar_order),nx=ncoef_x_B,nm=ncoef_m_B) - weight_B = (np.sqrt(len(orders)) / rms_ms)**2 - - shift = (p_shift_co_R[0]*weight_R + p_shift_co_B[0]*weight_B) / weight_R + weight_B - print p_shift_co_R,p_shift_co_B,shift - else: - p_shift_co_R = [0.] - p_shift_co_B = [0.] - p_shift = 0. - shift = 0. - - good_quality = True + good_quality = True hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR SHIFT_R',p_shift_co_R[0]) hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR SHIFT_B',p_shift_co_B[0]) - hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR SHIFT',shift) - + hdu = GLOBALutils.update_header(hdu,'HIERARCH THAR SHIFT',shift) + # Apply new wavelength solution including barycentric correction - equis = np.arange( dataB.shape[1] ) + equis = np.arange( dataB.shape[1] ) for order in range(nord_ob2): m = order + or0_R chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=or0_R,ntotal=nord_ob2,npix=len(equis),nx=ncoef_x_R,nm=ncoef_m_R) - WavSol = lbary_ltopo * (1.0 + 1.0e-6*shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_R'],chebs,ncoef_x_R,ncoef_m_R) + WavSol = lbary_ltopo * (1.0 + 1.0e-6*shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_R'],chebs,ncoef_x_R,ncoef_m_R) spec[0,order,:] = GLOBALutils.ToVacuum(WavSol) - spec[1,order,:] = sci_S_ob_R[order,1, :] + spec[1,order,:] = sci_S_ob_R[order,1, :] spec[2,order,:] = sci_S_ob_R[order,2, :] fn = R_flat_ob[order,1,:] L = np.where( fn > 0 )[0] @@ -1374,22 +1375,22 @@ for order in range(nord_ob1): m = order + or0_B chebs = GLOBALutils.Calculate_chebs(equis, m, Inverse=Inverse_m,order0=or0_B,ntotal=nord_ob1,npix=len(equis),nx=ncoef_x_B,nm=ncoef_m_B) - WavSol = lbary_ltopo * (1.0 + 1.0e-6*shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_B'],chebs,ncoef_x_B,ncoef_m_B) + WavSol = lbary_ltopo * (1.0 + 1.0e-6*shift) * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1_B'],chebs,ncoef_x_B,ncoef_m_B) spec[0,order + nord_ob2,:] = GLOBALutils.ToVacuum(WavSol) - spec[1,order + nord_ob2,:] = sci_S_ob_B[order,1, :] + spec[1,order + nord_ob2,:] = sci_S_ob_B[order,1, :] spec[2,order + nord_ob2,:] = sci_S_ob_B[order,2, :] fn = B_flat_ob[order,1,:] L = np.where( fn > 0 )[0] spec[3,order + nord_ob2,:][L] = sci_S_ob_B[order,1,:][L] / B_flat_ob[order,1,:][L] spec[4,order + nord_ob2,:][L] = sci_S_ob_B[order,2,:][L] * ( B_flat_ob[order,1,:][L] ** 2 ) - ccoefs = GLOBALutils.get_cont(spec[0],spec[3]) + ccoefs = GLOBALutils.get_cont(spec[0],spec[3]) - for order in range(nord_ob2): - fn = R_flat_ob_n[order,1,:] + for order in range(nord_ob2): + fn = R_flat_ob_n[order,1,:] L = np.where( (spec[1,order,:] != 0) & (fn > 0) )[0] - spec[5,order,:][L] = spec[3,order,:][L] / np.polyval(ccoefs[order],spec[0,order,:][L]) + spec[5,order,:][L] = spec[3,order,:][L] / np.polyval(ccoefs[order],spec[0,order,:][L]) ratio = np.polyval(ccoefs[order],spec[0,order,:][L])*Rnorms[order] spec[3,order,:][L] = sci_S_ob_R[order,1,:][L] / R_flat_ob_n[order,1,:][L] @@ -1401,15 +1402,15 @@ dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) dlambda_dx /= NN - LL = np.where(spec[5,order,:] > 1 + 10. / scipy.signal.medfilt(spec[8,order,:],21))[0] - spec[5,order,LL] = 1. - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + LL = np.where(spec[5,order,:] > 1 + 10. / scipy.signal.medfilt(spec[8,order,:],21))[0] + spec[5,order,LL] = 1. + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) - for order in range(nord_ob1): - fn = B_flat_ob_n[order,1,:] + for order in range(nord_ob1): + fn = B_flat_ob_n[order,1,:] L = np.where( (spec[1,order + nord_ob2,:] != 0) & (fn > 0) )[0] - spec[5,order + nord_ob2,:][L] = spec[3,order + nord_ob2,:][L] / np.polyval(ccoefs[order + nord_ob2],spec[0,order + nord_ob2,:][L]) + spec[5,order + nord_ob2,:][L] = spec[3,order + nord_ob2,:][L] / np.polyval(ccoefs[order + nord_ob2],spec[0,order + nord_ob2,:][L]) ratio = np.polyval(ccoefs[order + nord_ob2],spec[0,order + nord_ob2,:][L])*Bnorms[order] #L = np.where( fn > 0 ) @@ -1421,16 +1422,16 @@ spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) - dlambda_dx /= NN - LL = np.where(spec[5,order + nord_ob2,:] > 1 + 20./scipy.signal.medfilt(spec[8,order + nord_ob2,:],21))[0] - spec[5,order + nord_ob2,LL] = 1. - spec[9,order + nord_ob2,:][L] = spec[5,order + nord_ob2,:][L] * (dlambda_dx[L] ** 1) + dlambda_dx /= NN + LL = np.where(spec[5,order + nord_ob2,:] > 1 + 20./scipy.signal.medfilt(spec[8,order + nord_ob2,:],21))[0] + spec[5,order + nord_ob2,LL] = 1. + spec[9,order + nord_ob2,:][L] = spec[5,order + nord_ob2,:][L] * (dlambda_dx[L] ** 1) spec[10,order + nord_ob2,:][L] = spec[6,order + nord_ob2,:][L] / (dlambda_dx[L] ** 2) - JustExtract = False + JustExtract = False if (not JustExtract): if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') query_success,sp_type_query = False,'None' # spectral analysis #query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) @@ -1444,7 +1445,7 @@ pars_file = dirout + fsim.split('/')[-1][:-4]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./RESI**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -1455,12 +1456,12 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -1477,10 +1478,10 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) @@ -1501,9 +1502,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True if sp_type == 'M5': @@ -1516,40 +1517,40 @@ while (cond): # first rough correlation to find the minimum vels, xc_full, sn, nlines_ccf, W_ccf = \ - GLOBALutils.XCor(spec, ml_v, mh_v, weight,\ - 0, lbary_ltopo, vel_width=300, vel_step=3,\ - spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) + GLOBALutils.XCor(spec, ml_v, mh_v, weight,\ + 0, lbary_ltopo, vel_width=300, vel_step=3,\ + spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) - # Normalize the continuum of the CCF robustly with lowess + # Normalize the continuum of the CCF robustly with lowess yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) xc_av_orig = xc_av.copy() xc_av /= pred - vel0_xc = vels[ np.argmin( xc_av ) ] - + vel0_xc = vels[ np.argmin( xc_av ) ] + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = \ - vels.copy(), xc_av.copy(), pred.copy(),\ - xc_av_orig.copy(), vel0_xc + vels.copy(), xc_av.copy(), pred.copy(),\ + xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight,\ - vel0_xc, lbary_ltopo, vel_width=vel_width,\ - vel_step=0.1, spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) + GLOBALutils.XCor(spec, ml_v, mh_v, weight,\ + vel0_xc, lbary_ltopo, vel_width=vel_width,\ + vel_step=0.1, spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3.0, Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred p1,XCmodel,p1gau,XCmodelgau,Ls2 = \ - GLOBALutils.XC_Final_Fit( vels, xc_av, sigma_res = 4,\ - horder=8, moonv=refvel, moons=moon_sig, moon=False) + GLOBALutils.XC_Final_Fit( vels, xc_av, sigma_res = 4,\ + horder=8, moonv=refvel, moons=moon_sig, moon=False) moonmatters = False if (know_moon and here_moon): @@ -1557,7 +1558,7 @@ ismoon = True confused = False p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av, \ - sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) + sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) moon_flag = 1 else: confused = False @@ -1567,26 +1568,26 @@ SP = GLOBALutils.calc_bss2(vels,xc_av,p1gau) #SP = bspan[0] - + if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) @@ -1596,7 +1597,7 @@ if not avoid_plot: GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - SNR_5130 = np.median(spec[8,28,1900:2101] ) + SNR_5130 = np.median(spec[8,28,1900:2101] ) airmass = h[0].header['HIERARCH ESO TEL AIRM START'] seeing = h[0].header['HIERARCH ESO TEL AMBI FWHM START'] @@ -1615,13 +1616,13 @@ if BSerr<0.002: BSerr = .002 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1646,17 +1647,17 @@ hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f harps ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, RESI, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) + TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) else: - print "\t\tReading spectral file from", fout + print("\t\tReading spectral file from", fout) spec = pyfits.getdata( fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/harps/harpsutils.py b/harps/harpsutils.py index d52df06..c1e2deb 100644 --- a/harps/harpsutils.py +++ b/harps/harpsutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from astropy.io import fits as pyfits from pylab import * import scipy @@ -16,13 +17,13 @@ def getObName(h): """ obname = h[0].header['HIERARCH ESO OBS TARG NAME'].upper().replace(' ','') if 'ARCFILE' in h[0].header.keys(): - if h[0].header['ARCFILE'] == 'HARPS.2011-03-29T01:43:58.502.fits': - obname = 'HD77338' + if h[0].header['ARCFILE'] == 'HARPS.2011-03-29T01:43:58.502.fits': + obname = 'HD77338' return obname def FileClassify(diri, log, mode='HARPS'): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -37,60 +38,60 @@ def FileClassify(diri, log, mode='HARPS'): ThAr_ref_dates = [] obnames = [] exptimes = [] - co_types = [] + co_types = [] f = open(log,'w') #Do not consider the images specified in dir+badfiles.txt bad_files = [] if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + all_files = glob.glob(diri+"/HARPS*fits") for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - if h[0].header['HIERARCH ESO INS MODE'] == mode: - if h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'STAR,WAVE' or h[0].header['HIERARCH ESO DPR TYPE'][:8] == 'STAR,SKY' or h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'STAR,DARK': - co_type = h[0].header['HIERARCH ESO DPR TYPE'].split(',')[1] - if co_type == 'DARK': - co_type = 'SKY' - co_types.append(co_type) - sim_sci.append(archivo) - obname = getObName(h) - obnames.append( obname ) - ra = h[0].header['RA'] - delta = h[0].header['DEC'] - airmass= h[0].header['HIERARCH ESO TEL AIRM START'] - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'][:10] - hour = h[0].header['DATE-OBS'][11:] - exptimes.append( texp ) - line = "%-15s %8s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, co_type, ra, delta, texp, airmass, date, hour, archivo) - f.write(line) - elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'BIAS,BIAS': - biases.append(archivo) - elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'LAMP,DARK': - ob_flats.append(archivo) - elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'DARK,LAMP': - co_flats.append(archivo) - elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'LAMP,LAMP': - flats.append(archivo) - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'WAVE,WAVE,THAR2': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - ThAr_ref_dates.append( mjd ) - + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + if h[0].header['HIERARCH ESO INS MODE'] == mode: + if h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'STAR,WAVE' or h[0].header['HIERARCH ESO DPR TYPE'][:8] == 'STAR,SKY' or h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'STAR,DARK': + co_type = h[0].header['HIERARCH ESO DPR TYPE'].split(',')[1] + if co_type == 'DARK': + co_type = 'SKY' + co_types.append(co_type) + sim_sci.append(archivo) + obname = getObName(h) + obnames.append( obname ) + ra = h[0].header['RA'] + delta = h[0].header['DEC'] + airmass= h[0].header['HIERARCH ESO TEL AIRM START'] + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'][:10] + hour = h[0].header['DATE-OBS'][11:] + exptimes.append( texp ) + line = "%-15s %8s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, co_type, ra, delta, texp, airmass, date, hour, archivo) + f.write(line) + elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'BIAS,BIAS': + biases.append(archivo) + elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'LAMP,DARK': + ob_flats.append(archivo) + elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'DARK,LAMP': + co_flats.append(archivo) + elif h[0].header['HIERARCH ESO DPR TYPE'][:9] == 'LAMP,LAMP': + flats.append(archivo) + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'WAVE,WAVE,THAR2': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + ThAr_ref_dates.append( mjd ) + f.close() return biases, flats, ob_flats, co_flats, ThAr_ref, sim_sci, ThAr_ref_dates, obnames, exptimes, co_types @@ -101,7 +102,7 @@ def mjd_fromheader(h): """ secinday = 24*3600.0 - datetu = h[0].header['DATE-OBS'][:10] + datetu = h[0].header['DATE-OBS'][:10] ut = h[0].header['DATE-OBS'][11:] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:10])) @@ -137,10 +138,10 @@ def MedianCombine(ImgList, zero='none'): d2 = OverscanTrim(d2) if zero != 'none': - z = pyfits.open(zero)[0] - d1 -= z.data[:,:,0] - d2 -= z.data[:,:,1] - + z = pyfits.open(zero)[0] + d1 -= z.data[:,:,0] + d2 -= z.data[:,:,1] + factor = 1.25 if (n < 3): factor = 1 @@ -150,17 +151,17 @@ def MedianCombine(ImgList, zero='none'): ron1 = factor * ron1 / np.sqrt(n) ron2 = factor * ron2 / np.sqrt(n) if n>1: - for i in range(n-1): - td = pyfits.open(ImgList[i+1]) - if zero == 'none': - d1 = np.dstack((d1,OverscanTrim(td[1].data))) - d2 = np.dstack((d2,OverscanTrim(td[2].data))) - - else: - d1 = np.dstack((d1,OverscanTrim(td[1].data)-z.data[:,:,0])) - d2 = np.dstack((d2,OverscanTrim(td[2].data)-z.data[:,:,1])) - d1 = np.median(d1,axis=2) - d2 = np.median(d2,axis=2) + for i in range(n-1): + td = pyfits.open(ImgList[i+1]) + if zero == 'none': + d1 = np.dstack((d1,OverscanTrim(td[1].data))) + d2 = np.dstack((d2,OverscanTrim(td[2].data))) + + else: + d1 = np.dstack((d1,OverscanTrim(td[1].data)-z.data[:,:,0])) + d2 = np.dstack((d2,OverscanTrim(td[2].data)-z.data[:,:,1])) + d1 = np.median(d1,axis=2) + d2 = np.median(d2,axis=2) return np.dstack((d1,d2)), np.array([ron1,ron2]), np.array([gain1,gain2]) @@ -178,4 +179,4 @@ def OverscanTrim(d): overscan = np.array([overscan]*2048) overscan = overscan.transpose() newdata = data - overscan - return newdata.transpose() \ No newline at end of file + return newdata.transpose() diff --git a/harps/wavcals/make_multy.py b/harps/wavcals/make_multy.py index 85d035f..fd4a637 100644 --- a/harps/wavcals/make_multy.py +++ b/harps/wavcals/make_multy.py @@ -1,40 +1,39 @@ +from __future__ import print_function import numpy #inp = 'B_order_00.dat' ii = 10 while ii < 44: - inp = 'B_order_'+str(ii)+'.dat' - print inp - f = open(inp,'r') - lines = f.readlines() - f.close() - pix,wav, elm = [],[],[] - for line in lines: - cos = line.split() - pix.append(int(cos[1])) - wav.append(float(cos[2])) - elm.append(cos[3]) - vecs = [] - vec = [0] - i = 1 - while i < len(pix): - if pix[i] - pix[i-1] <= 8: - vec.append(i) - else: - vecs.append(vec) - vec=[i] - i+=1 - vecs.append(vec) - f = open(inp[:11]+'iwdat','w') - for vec in vecs: - lout = str(len(vec)) - for i in vec: - lout += '\t' + str(pix[i]) + '\t' + str(wav[i]) - for i in vec: - lout += '\t' + elm[i] - lout += '\n' - f.write(lout) - f.close() - ii+=1 - - + inp = 'B_order_'+str(ii)+'.dat' + print(inp) + f = open(inp,'r') + lines = f.readlines() + f.close() + pix,wav, elm = [],[],[] + for line in lines: + cos = line.split() + pix.append(int(cos[1])) + wav.append(float(cos[2])) + elm.append(cos[3]) + vecs = [] + vec = [0] + i = 1 + while i < len(pix): + if pix[i] - pix[i-1] <= 8: + vec.append(i) + else: + vecs.append(vec) + vec=[i] + i+=1 + vecs.append(vec) + f = open(inp[:11]+'iwdat','w') + for vec in vecs: + lout = str(len(vec)) + for i in vec: + lout += '\t' + str(pix[i]) + '\t' + str(wav[i]) + for i in vec: + lout += '\t' + elm[i] + lout += '\n' + f.write(lout) + f.close() + ii+=1 diff --git a/hires/hirespipe.py b/hires/hirespipe.py index 96c0aae..36c4ad4 100644 --- a/hires/hirespipe.py +++ b/hires/hirespipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * base = '../' @@ -84,7 +85,7 @@ force_flat_nor = False force_thar_extract = False force_thar_wavcal = False -force_tharxc = False +force_tharxc = False force_sci_extract = False force_spectral_file_build = True force_stellar_pars = False @@ -102,7 +103,7 @@ NSigma_Marsh = 5 NCosmic_Marsh = 5 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 0 max_extract_col = 4000 xbinning = 1 @@ -110,9 +111,9 @@ chip = 2 if chip == 2: - ro0 = 120 + ro0 = 120 -ext_aperture = int(float(ext_aperture)/float(ybinning)) +ext_aperture = int(float(ext_aperture)/float(ybinning)) ncoef_x = 4 ncoef_m = 5 @@ -122,48 +123,48 @@ ############################# -print "\n\n\tHIRES KECK10m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tHIRES KECK10m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # file containing the log log = dirout+'night.log' flats, ThAr_ref, sim_sci, ThAr_ref_dates, obnames, exptimes = hiresutils.FileClassify(dirin,log) -print '\tThis in the log of the night:\n' +print('\tThis in the log of the night:\n') f = open(log) flines = f.readlines() for line in flines: - print '\t\t'+line[:-1] -print '\n' + print('\t\t'+line[:-1]) +print('\n') if ((os.access(dirout+'Flat_' + str(int(chip)) + '.fits',os.F_OK) == False) or \ (os.access(dirout+'trace_' + str(int(chip)) + '.pkl',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine list of flats - print "\t\tGenerating Master calibration frames..." + print("\t\tGenerating Master calibration frames...") Flat, RO_fl, GA_fl = hiresutils.MedianCombine(flats,chip=chip) hdu = pyfits.PrimaryHDU(Flat) if (os.access(dirout+'Flat_' + str(int(chip)) + '.fits',os.F_OK)): os.remove(dirout+'Flat_' + str(int(chip)) + '.fits') hdu.writeto(dirout+'Flat_' + str(int(chip)) + '.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") hdu = pyfits.open(dirin+stst) d = hdu[chip].data d = hiresutils.OverscanTrim(d,hdu[chip].header['DATASEC']) d = d.T - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all, nord = GLOBALutils.get_them(d,ext_aperture+5,trace_degree,mode=1,nsigmas=10,nc2=1) - print '\t\t'+str(nord)+' orders found...' + print('\t\t'+str(nord)+' orders found...') # pickle traces trace_dict = {'c_all':c_all, @@ -182,147 +183,147 @@ h = pyfits.open(dirout+'Flat_' + str(int(chip)) + '.fits') Flat = h[0].data -print '\n\tNormalization of Flat calibration frame:' +print('\n\tNormalization of Flat calibration frame:') # Normalization of flat frame if (os.access(dirout+'NFlat_' + str(int(chip)) + '.fits',os.F_OK)==False) or (os.access(dirout+'Flat_spec_' + str(int(chip)) + '.fits',os.F_OK)==False) or force_flat_nor: - if (os.access(dirout+'BAC_Flat_' + str(int(chip)) + '.fits',os.F_OK)==False) or force_bac: - bac = hiresutils.bac_flat(Flat,c_all,ext_aperture) - if os.access(dirout+'BAC_Flat_' + str(int(chip)) + '.fits',os.F_OK): - os.system('rm '+dirout+'BAC_Flat_' + str(int(chip)) + '.fits') - hdu = pyfits.PrimaryHDU(bac) - hdu.writeto(dirout+'BAC_Flat_' + str(int(chip)) + '.fits') - else: - bac = pyfits.getdata(dirout+'BAC_Flat_' + str(int(chip)) + '.fits') - - ccdflat = hiresutils.ccd_flat(Flat,c_all,ext_aperture) - nflat,sflat = hiresutils.normalize_flat(Flat,c_all,ext_aperture) - hdu = pyfits.PrimaryHDU(nflat) - if (os.access(dirout+'NFlat_' + str(int(chip)) + '.fits',os.F_OK)): - os.remove(dirout+'NFlat_' + str(int(chip)) + '.fits') - hdu.writeto(dirout+'NFlat_' + str(int(chip)) + '.fits') - hdu = pyfits.PrimaryHDU(sflat) - if (os.access(dirout+'Flat_spec_' + str(int(chip)) + '.fits',os.F_OK)): - os.remove(dirout+'Flat_spec_' + str(int(chip)) + '.fits') - hdu.writeto(dirout+'Flat_spec_' + str(int(chip)) + '.fits') - + if (os.access(dirout+'BAC_Flat_' + str(int(chip)) + '.fits',os.F_OK)==False) or force_bac: + bac = hiresutils.bac_flat(Flat,c_all,ext_aperture) + if os.access(dirout+'BAC_Flat_' + str(int(chip)) + '.fits',os.F_OK): + os.system('rm '+dirout+'BAC_Flat_' + str(int(chip)) + '.fits') + hdu = pyfits.PrimaryHDU(bac) + hdu.writeto(dirout+'BAC_Flat_' + str(int(chip)) + '.fits') + else: + bac = pyfits.getdata(dirout+'BAC_Flat_' + str(int(chip)) + '.fits') + + ccdflat = hiresutils.ccd_flat(Flat,c_all,ext_aperture) + nflat,sflat = hiresutils.normalize_flat(Flat,c_all,ext_aperture) + hdu = pyfits.PrimaryHDU(nflat) + if (os.access(dirout+'NFlat_' + str(int(chip)) + '.fits',os.F_OK)): + os.remove(dirout+'NFlat_' + str(int(chip)) + '.fits') + hdu.writeto(dirout+'NFlat_' + str(int(chip)) + '.fits') + hdu = pyfits.PrimaryHDU(sflat) + if (os.access(dirout+'Flat_spec_' + str(int(chip)) + '.fits',os.F_OK)): + os.remove(dirout+'Flat_spec_' + str(int(chip)) + '.fits') + hdu.writeto(dirout+'Flat_spec_' + str(int(chip)) + '.fits') + else: - nflat = pyfits.getdata(dirout+'NFlat_' + str(int(chip)) + '.fits') - sflat = pyfits.getdata(dirout+'Flat_spec_' + str(int(chip)) + '.fits') + nflat = pyfits.getdata(dirout+'NFlat_' + str(int(chip)) + '.fits') + sflat = pyfits.getdata(dirout+'Flat_spec_' + str(int(chip)) + '.fits') -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in ThAr_ref: thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' if ( os.access(thar_fits,os.F_OK) == False ) or ( force_thar_extract ): - hthar = pyfits.open( fsim ) - dthar = hiresutils.OverscanTrim( hthar[chip].data, hthar[chip].header['DATASEC'] ) / nflat - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." - dthar = dthar.T - thar_S = np.zeros( (nord,dthar.shape[1]) ) - tR,tG = hthar[0].header['CCDRN0'+str(int(chip))],hthar[0].header['CCDGN0'+str(int(chip))] + hthar = pyfits.open( fsim ) + dthar = hiresutils.OverscanTrim( hthar[chip].data, hthar[chip].header['DATASEC'] ) / nflat + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") + dthar = dthar.T + thar_S = np.zeros( (nord,dthar.shape[1]) ) + tR,tG = hthar[0].header['CCDRN0'+str(int(chip))],hthar[0].header['CCDGN0'+str(int(chip))] - thar_S = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) + thar_S = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) # save as fits file if (os.access(thar_fits,os.F_OK)): - os.remove( thar_fits ) + os.remove( thar_fits ) hdu = pyfits.PrimaryHDU( thar_S ) hdu.writeto( thar_fits ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") for fsim in ThAr_ref: - hthar = pyfits.open( fsim ) - thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' - wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+ '.wavsolpars.pkl' - if ( os.access(wavsol_pkl,os.F_OK) == False ) or ( force_thar_wavcal ): - print "\t\tWorking on initial ThAr file", fsim - mjd, mjd0 = hiresutils.mjd_fromheader2( hthar[0].header ) - thar_hd = pyfits.getheader(fsim) - print "\t\tDecher Name:",thar_hd['DECKNAME'] - thar_S = pyfits.getdata( thar_fits ) - lines_thar = thar_S.copy() - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - - for order in range(len(c_all)): - order_s = str(order) - if (order < 10): - order_s = '0'+str(order) - thar_order_orig = lines_thar[order] - I = np.where(np.isnan(thar_order_orig)) - J = np.where(np.isnan(thar_order_orig)==False) - thar_order_orig[I] = np.median(thar_order_orig[J]) - thar_order = thar_order_orig - scipy.signal.medfilt(thar_order_orig,101) - wei = np.ones(len(thar_order)) - - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'echorder_'+\ - order_s+'_'+str(int(chip))+'.iwdat', thar_order, order, wei, rmsmax=100, \ - minlines=20,FixEnds=False,Dump_Argon=dumpargon,\ - Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x) - - if (chip == 2) and (order == 8): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*(len(thar_order)-1), len(thar_order)) - - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - - - isz = pixel_centers - sigmas - der = pixel_centers + sigmas - isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz, len(thar_order)) - der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der, len(thar_order)) - sig = 0.5*(der-isz) - fwhm = 2.35 * sig - resol = wavelengths / fwhm - #print order, np.median(resol) - #plot(pixel_centers,resol,'o') - #show() - - p0 = np.zeros( npar_wsol ) - p0[0] = (8+ro0) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ - np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ - maxrms=MRMS, Inv=Inverse_m,minlines=500,order0=ro0, \ - ntotal=nord,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ - 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas} - pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) - - else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl - -print '\n\tThe following targets will be processed:' + hthar = pyfits.open( fsim ) + thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' + wavsol_pkl = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+ '.wavsolpars.pkl' + if ( os.access(wavsol_pkl,os.F_OK) == False ) or ( force_thar_wavcal ): + print("\t\tWorking on initial ThAr file", fsim) + mjd, mjd0 = hiresutils.mjd_fromheader2( hthar[0].header ) + thar_hd = pyfits.getheader(fsim) + print("\t\tDecher Name:",thar_hd['DECKNAME']) + thar_S = pyfits.getdata( thar_fits ) + lines_thar = thar_S.copy() + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + + for order in range(len(c_all)): + order_s = str(order) + if (order < 10): + order_s = '0'+str(order) + thar_order_orig = lines_thar[order] + I = np.where(np.isnan(thar_order_orig)) + J = np.where(np.isnan(thar_order_orig)==False) + thar_order_orig[I] = np.median(thar_order_orig[J]) + thar_order = thar_order_orig - scipy.signal.medfilt(thar_order_orig,101) + wei = np.ones(len(thar_order)) + + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ + rms_ms, residuals, centroids, sigmas, intensities =\ + GLOBALutils.Initial_Wav_Calibration(order_dir+'echorder_'+\ + order_s+'_'+str(int(chip))+'.iwdat', thar_order, order, wei, rmsmax=100, \ + minlines=20,FixEnds=False,Dump_Argon=dumpargon,\ + Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x) + + if (chip == 2) and (order == 8): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*(len(thar_order)-1), len(thar_order)) + + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + + + isz = pixel_centers - sigmas + der = pixel_centers + sigmas + isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz, len(thar_order)) + der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der, len(thar_order)) + sig = 0.5*(der-isz) + fwhm = 2.35 * sig + resol = wavelengths / fwhm + #print order, np.median(resol) + #plot(pixel_centers,resol,'o') + #show() + + p0 = np.zeros( npar_wsol ) + p0[0] = (8+ro0) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ + np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ + maxrms=MRMS, Inv=Inverse_m,minlines=500,order0=ro0, \ + ntotal=nord,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ + 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas} + pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) + + else: + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) + +print('\n\tThe following targets will be processed:') new_list = [] if object2do == 'all': - new_list = sim_sci + new_list = sim_sci elif object2do == 'no_I2': - for fsim in sim_sci: - h = pyfits.open(fsim) - if h[0].header['IODIN'] == False: - new_list.append(fsim) + for fsim in sim_sci: + h = pyfits.open(fsim) + if h[0].header['IODIN'] == False: + new_list.append(fsim) else: - for fsim in sim_sci: - hd = pyfits.getheader(fsim) - if object2do in hd['TARGNAME']: - new_list.append(fsim) + for fsim in sim_sci: + hd = pyfits.getheader(fsim) + if object2do in hd['TARGNAME']: + new_list.append(fsim) for fsim in new_list: - hd = pyfits.getheader(fsim) - print '\t\t'+str(hd['TARGNAME']) + hd = pyfits.getheader(fsim) + print('\t\t'+str(hd['TARGNAME'])) # Does any image have a special requirement for dealing with the moonlight? @@ -346,8 +347,8 @@ # Extract all Science files for fsim in new_list: - print '\n' - print "\t--> Working on image: ", fsim + print('\n') + print("\t--> Working on image: ", fsim) know_moon = False if fsim.split('/')[-1] in spec_moon: I = np.where(fsim.split('/')[-1] == spec_moon)[0] @@ -357,31 +358,31 @@ h = pyfits.open(fsim) mjd,mjd0 = hiresutils.mjd_fromheader2(h[0].header) ronoise, gain = float(h[0].header['CCDRN0'+str(int(chip))]),float(h[0].header['CCDGN0'+str(int(chip))]) - + # Object name obname = h[0].header['TARGNAME'].replace(' ','') - print "\t\tObject name:",obname - print "\t\tDeckr Name = ", h[0].header['DECKNAME'] + print("\t\tObject name:",obname) + print("\t\tDeckr Name = ", h[0].header['DECKNAME']) # Open file, trim, overscan subtract and MasterBias subtract bac_fits = dirout + 'BAC_' + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' data = hiresutils.OverscanTrim( h[chip].data, h[chip].header['DATASEC'] ).T c_new,pshift = GLOBALutils.retrace( data, c_all, span=15 ) - print '\t\ty shift = ', pshift, 'pxs' + print('\t\ty shift = ', pshift, 'pxs') centers = np.zeros((len(c_new),nflat.shape[0])) for i in range(len(c_new)): - centers[i] = np.polyval(c_new[i],np.arange(nflat.shape[0])) - + centers[i] = np.polyval(c_new[i],np.arange(nflat.shape[0])) + force_bac=False if (os.access(bac_fits,os.F_OK) == False) or force_bac: - bac = GLOBALutils.get_scat(data,centers,span=10) - bach = pyfits.PrimaryHDU(bac) - if os.access(bac_fits,os.F_OK): - os.system('rm ' + bac_fits) - bach.writeto(bac_fits) + bac = GLOBALutils.get_scat(data,centers,span=10) + bach = pyfits.PrimaryHDU(bac) + if os.access(bac_fits,os.F_OK): + os.system('rm ' + bac_fits) + bach.writeto(bac_fits) else: - bac = pyfits.getdata(bac_fits) + bac = pyfits.getdata(bac_fits) data = data - bac data = data / nflat.T @@ -394,10 +395,10 @@ ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 + ra = ra2 + dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') # set observatory parameters altitude = 4145. @@ -416,16 +417,16 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() + gobs = ephem.Observer() gobs.name = 'Keck' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) gobs.date = h[0].header['DATE-OBS'] + ' ' + h[0].header['UTC'].replace(':','-') mephem = ephem.Moon() mephem.compute(gobs) @@ -435,31 +436,31 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel - + print('\t\tRadial Velocity of sacttered moonlight:',refvel) + sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' sci_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple_'+str(int(chip))+'.fits' P_fits = dirout + 'P_' + fsim.split('/')[-1][:-4]+'spec_'+str(int(chip))+'.fits' if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or \ ( force_sci_extract ): - - print "\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." - P = GLOBALutils.obtain_P(data,c_new,ext_aperture,ronoise,\ + print("\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") + + P = GLOBALutils.obtain_P(data,c_new,ext_aperture,ronoise,\ gain,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) + + sci_Ss = GLOBALutils.simple_extraction(data,c_new,ext_aperture,min_extract_col,max_extract_col,npools) + sci_S = GLOBALutils.optimal_extraction(data,P,c_new,ext_aperture,ronoise,gain,S_Marsh,NCosmic_Marsh,\ + min_extract_col,max_extract_col,npools) - sci_Ss = GLOBALutils.simple_extraction(data,c_new,ext_aperture,min_extract_col,max_extract_col,npools) - sci_S = GLOBALutils.optimal_extraction(data,P,c_new,ext_aperture,ronoise,gain,S_Marsh,NCosmic_Marsh,\ - min_extract_col,max_extract_col,npools) - if (os.access(sci_fits,os.F_OK)): os.remove( sci_fits ) if (os.access(sci_fits_simple,os.F_OK)): os.remove( sci_fits_simple ) - if (os.access(P_fits,os.F_OK)): + if (os.access(P_fits,os.F_OK)): os.remove( P_fits ) hdu = pyfits.PrimaryHDU( sci_S ) @@ -468,10 +469,10 @@ hdu.writeto( sci_fits_simple ) hdu = pyfits.PrimaryHDU( P ) hdu.writeto( P_fits ) - + else: - print '\t\t'+fsim+" has already been extracted, reading in product fits files..." + print('\t\t'+fsim+" has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) @@ -487,7 +488,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['UTC']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -498,111 +499,111 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',h[0].header['AIRMASS']) - hdu = GLOBALutils.update_header(hdu,'DECKNAME',h[0].header['DECKNAME']) - - models_path = base+"data/COELHO_MODELS/R_40000b/" - deckname = h[0].header['DECKNAME'] - if deckname in ['B1','B2','B3','B4']: - ref_RES = 72000. - elif deckname in ['B5','C1','C2','C3']: - ref_RES = 48000. - elif deckname in ['C4','C5','D1','D2']: - ref_RES = 36000. - else: - ref_RES = -999 + hdu = GLOBALutils.update_header(hdu,'DECKNAME',h[0].header['DECKNAME']) + + models_path = base+"data/COELHO_MODELS/R_40000b/" + deckname = h[0].header['DECKNAME'] + if deckname in ['B1','B2','B3','B4']: + ref_RES = 72000. + elif deckname in ['B5','C1','C2','C3']: + ref_RES = 48000. + elif deckname in ['C4','C5','D1','D2']: + ref_RES = 36000. + else: + ref_RES = -999 # get ThAr closest in time - im = np.argmin(np.absolute(np.array(ThAr_ref_dates) - mjd)) - pkl_wsol = dirout + ThAr_ref[im].split('/')[-1][:-4]+'spec_'+str(int(chip))+ '.wavsolpars.pkl' - print "\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + im = np.argmin(np.absolute(np.array(ThAr_ref_dates) - mjd)) + pkl_wsol = dirout + ThAr_ref[im].split('/')[-1][:-4]+'spec_'+str(int(chip))+ '.wavsolpars.pkl' + print("\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) + equis = np.arange( data.shape[1] ) for order in range(nord): #order = 16 - #print order + #print order m = order + ro0 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1],\ - order0=ro0, ntotal=nord, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) + order0=ro0, ntotal=nord, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) spec[0,order,:] = GLOBALutils.ToVacuum(WavSol) spec[1,order,:] = sci_S[order,1, :] - #spec[1,order,-100:] = 0. + #spec[1,order,-100:] = 0. spec[2,order,:] = sci_S[order,2, :] - spec[3,order,:] = spec[1,order,:] / sflat[order] - spec[4,order,:] = spec[2,order,:] * sflat[order] ** 2 - - - I = np.where(spec[1,order]!=0)[0] - cont_coef,x,y = hiresutils.get_cont(spec[0,order,I],scipy.signal.medfilt(spec[3,order,I],11),n=3) - #plot(spec[0,order],sflat[order]) - #show() - #cv = np.vstack((x,y)).T - #x_i,y_i = HIRESutils.bspline(cv, n=1000, degree=3, periodic=False) - #plot(x_i,y_i) - #show() - #print gfd - ratio = np.polyval(cont_coef, spec[0,order,:]) - - IN = np.where(np.isnan(spec[3,order])==False)[0] - pred = np.ones(len(spec[3,order])) - if len(IN)>100: - predt,xu,yu = hiresutils.get_cont2(spec[0,order,IN],spec[3,order,IN]) - #plot(spec[0,order,IN],spec[3,order,IN]) - #print spec[0,order,IN].shape ,pred.shape - #plot(spec[0,order,IN],predt,'k',linewidth=2.0) - #plot(spec[0,order,IN],ratio[IN],'r',linewidth=2.0) - pred[IN] = predt - else: - 'Nothing happens' - ratio = pred - #plot(spec[0,order,I],spec[3,order,I]) - #plot(spec[0,order],ratio) + spec[3,order,:] = spec[1,order,:] / sflat[order] + spec[4,order,:] = spec[2,order,:] * sflat[order] ** 2 + + + I = np.where(spec[1,order]!=0)[0] + cont_coef,x,y = hiresutils.get_cont(spec[0,order,I],scipy.signal.medfilt(spec[3,order,I],11),n=3) + #plot(spec[0,order],sflat[order]) + #show() + #cv = np.vstack((x,y)).T + #x_i,y_i = HIRESutils.bspline(cv, n=1000, degree=3, periodic=False) + #plot(x_i,y_i) + #show() + #print gfd + ratio = np.polyval(cont_coef, spec[0,order,:]) + + IN = np.where(np.isnan(spec[3,order])==False)[0] + pred = np.ones(len(spec[3,order])) + if len(IN)>100: + predt,xu,yu = hiresutils.get_cont2(spec[0,order,IN],spec[3,order,IN]) + #plot(spec[0,order,IN],spec[3,order,IN]) + #print spec[0,order,IN].shape ,pred.shape + #plot(spec[0,order,IN],predt,'k',linewidth=2.0) + #plot(spec[0,order,IN],ratio[IN],'r',linewidth=2.0) + pred[IN] = predt + else: + 'Nothing happens' + ratio = pred + #plot(spec[0,order,I],spec[3,order,I]) + #plot(spec[0,order],ratio) L = np.where( spec[1,order,:] != 0 ) spec[5,order,:][L] = spec[3,order,:][L] / ratio[L] - spec[3,order,:][L] = spec[3,order,:][L] / ratio[L] - #spec[3,order,:][L] = spec[5,order,:][L].copy() + spec[3,order,:][L] = spec[3,order,:][L] / ratio[L] + #spec[3,order,:][L] = spec[5,order,:][L].copy() spec[6,order,:][L] = spec[4,order,:][L] * (ratio[L] ** 2 ) spec[7,order,:][L] = ratio[L] spec[8,order,:][L] = ratio[L] * sflat[order][L] / np.sqrt( ratio[L] * sflat[order][L] / gain + (ronoise/gain)**2 ) - rI = np.where(spec[5,order] > 1. + 8./spec[8,order]) - spec[5,order,rI] = 1. - #plot(spec[0,order],spec[5,order]) + rI = np.where(spec[5,order] > 1. + 8./spec[8,order]) + spec[5,order,rI] = 1. + #plot(spec[0,order],spec[5,order]) spl = scipy.interpolate.splrep(np.arange(len(WavSol)), WavSol,k=3) dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) dlambda_dx /= NN # clean-up of CRs in continuum-normalized spectrum. Set troublesome pixels to 1 - medflx = np.zeros(len(spec[5,order])) - nI = np.where(np.isnan(spec[5,order])==False)[0] - nJ = np.where(np.isnan(spec[5,order])==True)[0] + medflx = np.zeros(len(spec[5,order])) + nI = np.where(np.isnan(spec[5,order])==False)[0] + nJ = np.where(np.isnan(spec[5,order])==True)[0] spec[6,order,:][I] = spec[8,order,:][I] ** 2 - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) - #show() - #for order in range(nord): - # plot(spec[0,order],spec[5,order]) - #show() + #show() + #for order in range(nord): + # plot(spec[0,order],spec[5,order]) + #show() if (not JustExtract): if DoClass: # spectral analysis - print "\t\tSpectral Analysis..." + print("\t\tSpectral Analysis...") query_success = False # First, query SIMBAD with the object name query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + fsim.split('/')[-1][:-4]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") spec2 = spec.copy() if ref_RES>45000: Rx = np.around(1./np.sqrt(1./40000.**2 - 1./ref_RES**2)) @@ -614,12 +615,12 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -635,19 +636,19 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - + print("\t\t\tWill use",sp_type,"mask for CCF.") + velw = 300 velsh = 3. - + # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2.5 lower res in FEROS w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= 1.5*(av_m - ml_v) @@ -666,9 +667,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): # first rough correlation to find the minimum @@ -686,17 +687,17 @@ rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.2,\ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) - + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred - + if sp_type == 'M5': moon_sig = 2.5 elif sp_type == 'K5': @@ -719,18 +720,18 @@ p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 moon_flag = 0 - print 'what!!' + print('what!!') SP = GLOBALutils.calc_bss2(vels,xc_av,p1gau) #SP = bspan[0] #print p1gau[1] if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -738,13 +739,13 @@ BSerr = -999.00 xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' @@ -758,7 +759,7 @@ airmass = h[0].header['AIRMASS'] seeing = -999 - TEXP = h[0].header['EXPTIME'] + TEXP = h[0].header['EXPTIME'] SNR_5130 = np.median(spec[8,13,1900:2101] ) if sp_type=='G2': @@ -778,12 +779,12 @@ BSerr = D / float(np.round(SNR_5130)) + C RVerr = 0.5 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -810,10 +811,10 @@ hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f hires ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, ref_RES, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) + TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/hires/hiresutils.py b/hires/hiresutils.py index db1923b..97b61e9 100644 --- a/hires/hiresutils.py +++ b/hires/hiresutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") import numpy as np import scipy from scipy import signal,interpolate @@ -18,7 +19,7 @@ def FileClassify(diri, log): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -37,53 +38,53 @@ def FileClassify(diri, log): #Do not consider the images specified in dir+badfiles.txt bad_files = [] if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + all_files = glob.glob(diri+"/*fits") for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - hd = pyfits.getheader(archivo) - if h[0].header['IMAGETYP'] == 'object': - sim_sci.append(archivo) - obname = hd['TARGNAME'] - obnames.append( obname ) - ra = hd['RA'] - delta = hd['DEC'] - airmass= hd['AIRMASS'] - texp = hd['EXPTIME'] - date = hd['DATE-OBS'] - hour = hd['UTC'] - exptimes.append( texp ) - #rint obname, ra, delta, texp, airmass, date, hour, archivo - line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, float(texp), float(airmass), date, hour, archivo) - f.write(line) - elif h[0].header['IMAGETYP'] == 'flatlamp': - flats.append(archivo) - mjd, mjd0 = mjd_fromheader2(hd) - flat_ref_dates.append( mjd ) - elif h[0].header['IMAGETYP'] == 'arclamp': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader2(hd) - ThAr_ref_dates.append( mjd ) - + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + hd = pyfits.getheader(archivo) + if h[0].header['IMAGETYP'] == 'object': + sim_sci.append(archivo) + obname = hd['TARGNAME'] + obnames.append( obname ) + ra = hd['RA'] + delta = hd['DEC'] + airmass= hd['AIRMASS'] + texp = hd['EXPTIME'] + date = hd['DATE-OBS'] + hour = hd['UTC'] + exptimes.append( texp ) + #rint obname, ra, delta, texp, airmass, date, hour, archivo + line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, float(texp), float(airmass), date, hour, archivo) + f.write(line) + elif h[0].header['IMAGETYP'] == 'flatlamp': + flats.append(archivo) + mjd, mjd0 = mjd_fromheader2(hd) + flat_ref_dates.append( mjd ) + elif h[0].header['IMAGETYP'] == 'arclamp': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader2(hd) + ThAr_ref_dates.append( mjd ) + flat_ref_dates,ThAr_ref,ThAr_ref_dates = np.array(flat_ref_dates),np.array(ThAr_ref),np.array(ThAr_ref_dates) flats = np.array(flats) IS = np.argsort(flat_ref_dates) flat_ref_dates = flat_ref_dates[IS] flats = flats[IS] #for i in range(len(flats)): - # print 'flat',flats[i], flat_ref_dates[i] + # print 'flat',flats[i], flat_ref_dates[i] IS = np.argsort(ThAr_ref_dates) ThAr_ref_dates = ThAr_ref_dates[IS] ThAr_ref = ThAr_ref[IS] @@ -99,8 +100,8 @@ def mjd_fromheader2(hd): secinday = 24*3600.0 date = hd['DATE-OBS'] - ut = hd['UTC'] - + ut = hd['UTC'] + datetu = date.replace('-',':') mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:10])) @@ -129,7 +130,7 @@ def MedianCombine(ImgList, chip=1): h1 = h[0].header #d1 = b_col(d1) d1 = OverscanTrim(d1,h[chip].header['DATASEC']) - + factor = 1.25 if (n < 3): factor = 1 @@ -138,13 +139,13 @@ def MedianCombine(ImgList, chip=1): ron1 = factor * ron1 / np.sqrt(n) if n>1: - for i in range(n-1): - td = pyfits.open(ImgList[i+1]) - #print ImgList[i+1],td[2].data.shape - td = OverscanTrim(td[chip].data,td[chip].header['DATASEC']) - d1 = np.dstack((d1,td)) - - d1 = np.median(d1,axis=2) + for i in range(n-1): + td = pyfits.open(ImgList[i+1]) + #print ImgList[i+1],td[2].data.shape + td = OverscanTrim(td[chip].data,td[chip].header['DATASEC']) + d1 = np.dstack((d1,td)) + + d1 = np.median(d1,axis=2) return d1, ron1, gain1 @@ -167,197 +168,197 @@ def OverscanTrim(d,ds): return newdata def bac_flat(flat,traces,ext): - ext = int(np.around(.5*ext)) - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - bac = np.zeros(flat.shape) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - for px in np.arange(flat.shape[0]): - cut = flat[px] - cut = scipy.signal.medfilt(cut,11) - refx,refy = [],[] - for i in np.arange(len(traces)-1)+1: - """ - if i == 0: - ncut = cut[peaks[i,px]-40:peaks[i,px]] - im = peaks[i,px]-40 + np.argmin(ncut) - refx.append(im) - refy.append(cut[im]) - """ - ncut = cut[peaks[i-1,px]:peaks[i,px]] - im = peaks[i-1,px] + np.argmin(ncut) - refx.append(im) - refy.append(cut[im]) - refx,refy = np.array(refx),np.array(refy) - tck = scipy.interpolate.splrep(refx,refy,k=1) - bac[px] = scipy.interpolate.splev(np.arange(flat.shape[1]),tck) - bac[px,:refx[0]] = refy[0] - bac[px,refx[-1]:] = refy[-1] - bac = scipy.signal.medfilt(bac,[11,1]) - return bac - + ext = int(np.around(.5*ext)) + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + bac = np.zeros(flat.shape) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + for px in np.arange(flat.shape[0]): + cut = flat[px] + cut = scipy.signal.medfilt(cut,11) + refx,refy = [],[] + for i in np.arange(len(traces)-1)+1: + """ + if i == 0: + ncut = cut[peaks[i,px]-40:peaks[i,px]] + im = peaks[i,px]-40 + np.argmin(ncut) + refx.append(im) + refy.append(cut[im]) + """ + ncut = cut[peaks[i-1,px]:peaks[i,px]] + im = peaks[i-1,px] + np.argmin(ncut) + refx.append(im) + refy.append(cut[im]) + refx,refy = np.array(refx),np.array(refy) + tck = scipy.interpolate.splrep(refx,refy,k=1) + bac[px] = scipy.interpolate.splev(np.arange(flat.shape[1]),tck) + bac[px,:refx[0]] = refy[0] + bac[px,refx[-1]:] = refy[-1] + bac = scipy.signal.medfilt(bac,[11,1]) + return bac + def ccd_flat(flat,traces,ext): - print ext - nflat = np.ones(flat.shape) - ext = int(np.around(ext)) - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - for px in np.arange(flat.shape[0]): - for py in range(len(traces)): - tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] - nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf - - return nflat - + print(ext) + nflat = np.ones(flat.shape) + ext = int(np.around(ext)) + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + for px in np.arange(flat.shape[0]): + for py in range(len(traces)): + tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] + nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf + + return nflat + def normalize_flat(flat,traces,ext): - nflat = np.ones(flat.shape) - ext = int(np.around(.5*ext))+2 - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - spec_flat = np.zeros((len(traces),flat.shape[0])) - - for px in np.arange(flat.shape[0]): - for py in range(len(traces)): - tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] - tf2 = flat[px,peaks[py,px]-int(np.around(ext/2.)):peaks[py,px]+int(np.around(ext/2.))+1] - nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf / np.median(tf) - spec_flat[py,px] = np.median(tf2) - #for i in range(len(traces)): - # plot(spec_flat[i]) - # show() - for i in range(len(traces)): - ss = scipy.signal.medfilt(spec_flat[i],15) - spec_flat[i] /= ss.max() - #spec_flat_o = spec_flat.copy() - #for i in range(len(traces)): - # spec_flat[i] = spec_flat[i]/scipy.signal.medfilt(spec_flat[i],301) - # spec_flat[i] = spec_flat[i]/sm.max() - return nflat,spec_flat + nflat = np.ones(flat.shape) + ext = int(np.around(.5*ext))+2 + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + spec_flat = np.zeros((len(traces),flat.shape[0])) + + for px in np.arange(flat.shape[0]): + for py in range(len(traces)): + tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] + tf2 = flat[px,peaks[py,px]-int(np.around(ext/2.)):peaks[py,px]+int(np.around(ext/2.))+1] + nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf / np.median(tf) + spec_flat[py,px] = np.median(tf2) + #for i in range(len(traces)): + # plot(spec_flat[i]) + # show() + for i in range(len(traces)): + ss = scipy.signal.medfilt(spec_flat[i],15) + spec_flat[i] /= ss.max() + #spec_flat_o = spec_flat.copy() + #for i in range(len(traces)): + # spec_flat[i] = spec_flat[i]/scipy.signal.medfilt(spec_flat[i],301) + # spec_flat[i] = spec_flat[i]/sm.max() + return nflat,spec_flat def get_cont(x,y,n=1,sl=1.,sh=5.): - orilen = len(x) - coef = np.polyfit(x,y,n) - res = y - np.polyval(coef,x) - IH = np.where(res>0)[0] - IL = np.where(res<0)[0] - dev = np.mean(res[IH]) - I = np.where((res>-sl*dev) & (res=sh*dev)[0] - J = np.unique(np.hstack((J1,J2))) - cond = True - if len(J)==0 or len(x)< .3*orilen: - cond=False - while cond: - x = np.delete(x,J) - y = np.delete(y,J) - coef = np.polyfit(x,y,n) - res = y - np.polyval(coef,x) - IH = np.where(res>0)[0] - IL = np.where(res<0)[0] - dev = np.mean(res[IH]) - I = np.where((res>-sl*dev) & (res=sh*dev)[0] - J = np.unique(np.hstack((J1,J2))) - cond = True - if len(J)==0 or len(x)< .1*orilen: - cond=False - return coef,x,y + orilen = len(x) + coef = np.polyfit(x,y,n) + res = y - np.polyval(coef,x) + IH = np.where(res>0)[0] + IL = np.where(res<0)[0] + dev = np.mean(res[IH]) + I = np.where((res>-sl*dev) & (res=sh*dev)[0] + J = np.unique(np.hstack((J1,J2))) + cond = True + if len(J)==0 or len(x)< .3*orilen: + cond=False + while cond: + x = np.delete(x,J) + y = np.delete(y,J) + coef = np.polyfit(x,y,n) + res = y - np.polyval(coef,x) + IH = np.where(res>0)[0] + IL = np.where(res<0)[0] + dev = np.mean(res[IH]) + I = np.where((res>-sl*dev) & (res=sh*dev)[0] + J = np.unique(np.hstack((J1,J2))) + cond = True + if len(J)==0 or len(x)< .1*orilen: + cond=False + return coef,x,y def get_cont2(x,y,sl=2.,sh=5.): - orilen = len(x) - xo = x.copy() - yo = y.copy() - #J1 = np.where((x>5870) & (x<5910))[0] - #x = np.delete(x,J1) - #y = np.delete(y,J1) - - #J2 = np.where((x>5160) & (x<5210))[0] - #x = np.delete(x,J2) - #y = np.delete(y,J2) - - yy = scipy.signal.medfilt(y,11) - #lowess = robjects.r("lowess") - #approx = robjects.r("approx") - #Temp = lowess(x,yy,f=0.1,iter=10) - #pred = np.array( approx(Temp[0],Temp[1],xout=x, method="linear", rule=2) )[1] - - pred = lowess(yy, x,frac=0.2,it=10,return_sorted=False) - - res = y - pred - IH = np.where(res>0)[0] - IL = np.where(res<0)[0] - dev = np.mean(res[IH]) - I = np.where((res>-sl*dev) & (res=sh*dev)[0] - J = np.unique(np.hstack((J1,J2))) - cond = True - if len(J)==0 or len(x)< .3*orilen: - cond=False - #cond = False - - while cond: - x = np.delete(x,J) - y = np.delete(y,J) - - yy = scipy.signal.medfilt(y,11) - #lowess = robjects.r("lowess") - #approx = robjects.r("approx") - #Temp = lowess(x,yy,f=0.2,iter=10) - #pred = np.array( approx(Temp[0],Temp[1],xout=x, method="linear", rule=2) )[1] - pred = lowess(yy, x,frac=0.2,it=10,return_sorted=False) - - res = y - pred - IH = np.where(res>0)[0] - IL = np.where(res<0)[0] - dev = np.mean(res[IH]) - I = np.where((res>-sl*dev) & (res=sh*dev)[0] - J = np.sort(np.unique(np.hstack((J1,J2)))) - cond = True - if len(J)==0 or len(x)< .1*orilen: - cond=False - #pred = np.array( approx(Temp[0],Temp[1],xout=xo, method="linear", rule=2) )[1] - tck1 = scipy.interpolate.splrep(x,pred,k=1) - pred = scipy.interpolate.splev(xo,tck1) - #plot(xo,yo) - #plot(xo,pred) - #show() - #pred = lowess(yy, x,frac=0.4,it=10,return_sorted=False) - return pred,x,y + orilen = len(x) + xo = x.copy() + yo = y.copy() + #J1 = np.where((x>5870) & (x<5910))[0] + #x = np.delete(x,J1) + #y = np.delete(y,J1) + + #J2 = np.where((x>5160) & (x<5210))[0] + #x = np.delete(x,J2) + #y = np.delete(y,J2) + + yy = scipy.signal.medfilt(y,11) + #lowess = robjects.r("lowess") + #approx = robjects.r("approx") + #Temp = lowess(x,yy,f=0.1,iter=10) + #pred = np.array( approx(Temp[0],Temp[1],xout=x, method="linear", rule=2) )[1] + + pred = lowess(yy, x,frac=0.2,it=10,return_sorted=False) + + res = y - pred + IH = np.where(res>0)[0] + IL = np.where(res<0)[0] + dev = np.mean(res[IH]) + I = np.where((res>-sl*dev) & (res=sh*dev)[0] + J = np.unique(np.hstack((J1,J2))) + cond = True + if len(J)==0 or len(x)< .3*orilen: + cond=False + #cond = False + + while cond: + x = np.delete(x,J) + y = np.delete(y,J) + + yy = scipy.signal.medfilt(y,11) + #lowess = robjects.r("lowess") + #approx = robjects.r("approx") + #Temp = lowess(x,yy,f=0.2,iter=10) + #pred = np.array( approx(Temp[0],Temp[1],xout=x, method="linear", rule=2) )[1] + pred = lowess(yy, x,frac=0.2,it=10,return_sorted=False) + + res = y - pred + IH = np.where(res>0)[0] + IL = np.where(res<0)[0] + dev = np.mean(res[IH]) + I = np.where((res>-sl*dev) & (res=sh*dev)[0] + J = np.sort(np.unique(np.hstack((J1,J2)))) + cond = True + if len(J)==0 or len(x)< .1*orilen: + cond=False + #pred = np.array( approx(Temp[0],Temp[1],xout=xo, method="linear", rule=2) )[1] + tck1 = scipy.interpolate.splrep(x,pred,k=1) + pred = scipy.interpolate.splev(xo,tck1) + #plot(xo,yo) + #plot(xo,pred) + #show() + #pred = lowess(yy, x,frac=0.4,it=10,return_sorted=False) + return pred,x,y def bspline(points, n=100, degree=3, periodic=False): - x = points[:,0] - y = points[:,1] + x = points[:,0] + y = points[:,1] - t = range(len(points)) - ipl_t = np.linspace(0.0, len(points) - 1, n) + t = range(len(points)) + ipl_t = np.linspace(0.0, len(points) - 1, n) - x_tup = scipy.interpolate.splrep(t, x, k=3) - y_tup = scipy.interpolate.splrep(t, y, k=3) + x_tup = scipy.interpolate.splrep(t, x, k=3) + y_tup = scipy.interpolate.splrep(t, y, k=3) - x_list = list(x_tup) - xl = x.tolist() - x_list[1] = xl + [0.0, 0.0, 0.0, 0.0] + x_list = list(x_tup) + xl = x.tolist() + x_list[1] = xl + [0.0, 0.0, 0.0, 0.0] - y_list = list(y_tup) - yl = y.tolist() - y_list[1] = yl + [0.0, 0.0, 0.0, 0.0] + y_list = list(y_tup) + yl = y.tolist() + y_list[1] = yl + [0.0, 0.0, 0.0, 0.0] - x_i = scipy.interpolate.splev(ipl_t, x_list) - y_i = scipy.interpolate.splev(ipl_t, y_list) - return x_i,y_i \ No newline at end of file + x_i = scipy.interpolate.splev(ipl_t, x_list) + y_i = scipy.interpolate.splev(ipl_t, y_list) + return x_i,y_i diff --git a/install.py b/install.py index 1bacab9..871638a 100644 --- a/install.py +++ b/install.py @@ -1,11 +1,12 @@ +from __future__ import print_function ######################## Install Script v.1.0. ######################### # # # This script installs CERES in your computer # -# # -# UPDATES: # -# # +# # +# UPDATES: # +# # # 02-09-2016: Finished code. Working like a charm! # -# # +# # ######################################################################## p_name = "CERES" @@ -20,25 +21,25 @@ import update_ssephem def CheckLibraries(): - print " ----------------------------------------------------------" + print(" ----------------------------------------------------------") try: - import numpy + import numpy except ImportError: - print " ----------------------------------------------------------" - print ' ERROR: '+p_name+' will not be installed in your system because' - print ' numpy is not installed in your system.' - print ' To install it, go to: http://www.numpy.org/\n\n' - sys.exit(1) - print " > Numpy is ok!" + print(" ----------------------------------------------------------") + print(' ERROR: '+p_name+' will not be installed in your system because') + print(' numpy is not installed in your system.') + print(' To install it, go to: http://www.numpy.org/\n\n') + sys.exit(1) + print(" > Numpy is ok!") try: - import scipy + import scipy except ImportError: - print " ----------------------------------------------------------" - print ' ERROR: '+p_name+' will not be installed in your system because' - print ' scipy is not installed in your system.' - print ' To install it, go to: http://www.scipy.org/\n\n' - sys.exit(1) - print " > Scipy is ok!" + print(" ----------------------------------------------------------") + print(' ERROR: '+p_name+' will not be installed in your system because') + print(' scipy is not installed in your system.') + print(' To install it, go to: http://www.scipy.org/\n\n') + sys.exit(1) + print(" > Scipy is ok!") #try: # import pyfits #except ImportError: @@ -57,11 +58,11 @@ def spaced(input,space): input = space+input while(not fixed): if(input[i:i+1] == '\n'): - input = input[0:i+1]+space+input[i+1:] - i = i + len(space) + input = input[0:i+1]+space+input[i+1:] + i = i + len(space) i = i + 1 if(i == len(input)-1): - fixed = True + fixed = True return input def Build(directory): @@ -77,126 +78,125 @@ def Build(directory): pf2py = 'Proceso_f2py' stp = 'setup.py' if( cf[-len(pf2py):] == pf2py ): - print " > Fortran code found in directory "+directory+". Building..." - cwd = os.getcwd() - os.chdir(directory) - subprocess.Popen('chmod u+wrx Proceso_f2py',shell = True).wait() - subprocess.Popen('chmod g+wrx Proceso_f2py',shell = True).wait() - subprocess.Popen('chmod o+wrx Proceso_f2py',shell = True).wait() - p = subprocess.Popen('./Proceso_f2py',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print " ----------------------------------------------------------" - print " > ERROR: "+p_name+" couldn't be installed." - print " > Problem building code in "+directory+". The error was:\n" - out, err = p.communicate() - print err - print " > If you can't solve the problem, please communicate" - print " > with the "+p_name+" team for help.\n\n" - os.chdir(cwd) - sys.exit() - os.chdir(cwd) - print " >...done!" + print(" > Fortran code found in directory "+directory+". Building...") + cwd = os.getcwd() + os.chdir(directory) + subprocess.Popen('chmod u+wrx Proceso_f2py',shell = True).wait() + subprocess.Popen('chmod g+wrx Proceso_f2py',shell = True).wait() + subprocess.Popen('chmod o+wrx Proceso_f2py',shell = True).wait() + p = subprocess.Popen('./Proceso_f2py',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(" ----------------------------------------------------------") + print(" > ERROR: "+p_name+" couldn't be installed.") + print(" > Problem building code in "+directory+". The error was:\n") + out, err = p.communicate() + print(err) + print(" > If you can't solve the problem, please communicate") + print(" > with the "+p_name+" team for help.\n\n") + os.chdir(cwd) + sys.exit() + os.chdir(cwd) + print(" >...done!") elif( cf[-len(stp):] == stp ): - SetupFileFound = True + SetupFileFound = True elif( cf[-2:] == '.c' ): - CFileFound = True + CFileFound = True if( SetupFileFound and CFileFound ): - print " > C code found in directory "+directory+". Building..." - cwd = os.getcwd() - os.chdir(directory) - p = subprocess.Popen('{python} setup.py build'.format(python=sys.executable),stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print " ----------------------------------------------------------" - print " > ERROR: "+p_name+" couldn't be installed." - print " > Problem building code in "+directory+". The error was:\n" - out, err = p.communicate() - print spaced(err,"\t \t") - print " > If you can't solve the problem, please communicate" - print " > with the "+p_name+" team for help.\n \n" - os.chdir(cwd) - sys.exit() - libfolder = getDirs('build/.') - for name in libfolder: - if(name[0:3]=='lib'): - filename = glob.glob('build/'+name+'/*') - shutil.copy2(filename[0],'.') - shutil.rmtree('build') - os.chdir(cwd) - print ' >...done!' - # If the current file or folder is a directory, we apply the same + print(" > C code found in directory "+directory+". Building...") + cwd = os.getcwd() + os.chdir(directory) + p = subprocess.Popen('{python} setup.py build'.format(python=sys.executable),stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(" ----------------------------------------------------------") + print(" > ERROR: "+p_name+" couldn't be installed.") + print(" > Problem building code in "+directory+". The error was:\n") + out, err = p.communicate() + print(spaced(err,"\t \t")) + print(" > If you can't solve the problem, please communicate") + print(" > with the "+p_name+" team for help.\n \n") + os.chdir(cwd) + sys.exit() + libfolder = getDirs('build/.') + for name in libfolder: + if(name[0:3]=='lib'): + filename = glob.glob('build/'+name+'/*') + shutil.copy2(filename[0],'.') + shutil.rmtree('build') + os.chdir(cwd) + print(' >...done!') + # If the current file or folder is a directory, we apply the same # code to it: elif( os.path.isdir(cf) ): - Build(cf) -print " \n\n "+p_name+" Installer v.1.0. \n\n" -print " The "+p_name+" team is composed of: \n" -print " - Rafael Brahm (PUC, rabrahm@astro.puc.cl)." -print " - Andres Jordan (PUC, ajordan@astro.puc.cl)." -print " - Nestor Espinoza (PUC, nespino@astro.puc.cl). \n" -print " DISCLAIMER: If you use this pipeline or part of it, please " -print " acknowledge us and our current institutions. If you find any bugs," -print " please contact us. \n" -print " 1.- Preparing to install. Checking if your system has the libraries" -print " needed to compile the codes...\n" + Build(cf) +print(" \n\n "+p_name+" Installer v.1.0. \n\n") +print(" The "+p_name+" team is composed of: \n") +print(" - Rafael Brahm (PUC, rabrahm@astro.puc.cl).") +print(" - Andres Jordan (PUC, ajordan@astro.puc.cl).") +print(" - Nestor Espinoza (PUC, nespino@astro.puc.cl). \n") +print(" DISCLAIMER: If you use this pipeline or part of it, please ") +print(" acknowledge us and our current institutions. If you find any bugs,") +print(" please contact us. \n") +print(" 1.- Preparing to install. Checking if your system has the libraries") +print(" needed to compile the codes...\n") CheckLibraries() -print " ---------------------------------------------------------- \n" -print " Done! All libraries checked. \n" +print(" ---------------------------------------------------------- \n") +print(" Done! All libraries checked. \n") import numpy -print " 2.- Building processes...\n" -print " ----------------------------------------------------------" +print(" 2.- Building processes...\n") +print(" ----------------------------------------------------------") # First, we get all the directories in the current folder: dirs = getDirs('utils/') ndirs = [] for dire in dirs: - ndirs.append('utils/'+dire) + ndirs.append('utils/'+dire) dirs = ndirs for directory in dirs: # To each directory, we apply the build function: if(directory == 'utils/SSEphem'): - print " > Installing SSEphem...\n \n" - os.chdir('utils/SSEphem/SOFA') - os.system('mkdir lib') - os.system('mkdir include') - p = subprocess.Popen('make',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print ' Error in SSEphem installation! The error was:' - out, err = p.communicate() - print spaced(err,"\t \t") - sys.exit() - p = subprocess.Popen('make test',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print ' Error in SSEphem installation! The error was:' - out, err = p.communicate() - print spaced(err,"\t \t") - sys.exit() - os.chdir('../') - p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print ' Error in SSEphem installation! The error was:' - out, err = p.communicate() - print spaced(err,"\t \t") - sys.exit() - p = subprocess.Popen('make',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) - p.wait() - if(p.returncode != 0 and p.returncode != None): - print ' Error in SSEphem installation! The error was:' - out, err = p.communicate() - print spaced(err,"\t \t") - sys.exit() - else: - update_ssephem.SSEphemDownload() - update_ssephem.LeapSecUpdate() - update_ssephem.IersUpdate() + print(" > Installing SSEphem...\n \n") + os.chdir('utils/SSEphem/SOFA') + os.system('mkdir lib') + os.system('mkdir include') + p = subprocess.Popen('make',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(' Error in SSEphem installation! The error was:') + out, err = p.communicate() + print(spaced(err,"\t \t")) + sys.exit() + p = subprocess.Popen('make test',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(' Error in SSEphem installation! The error was:') + out, err = p.communicate() + print(spaced(err,"\t \t")) + sys.exit() + os.chdir('../') + p = subprocess.Popen('make clean',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(' Error in SSEphem installation! The error was:') + out, err = p.communicate() + print(spaced(err,"\t \t")) + sys.exit() + p = subprocess.Popen('make',stdout = subprocess.PIPE, stderr = subprocess.PIPE,shell = True) + p.wait() + if(p.returncode != 0 and p.returncode != None): + print(' Error in SSEphem installation! The error was:') + out, err = p.communicate() + print(spaced(err,"\t \t")) + sys.exit() + else: + update_ssephem.SSEphemDownload() + update_ssephem.LeapSecUpdate() + update_ssephem.IersUpdate() - os.chdir('../../') + os.chdir('../../') else: - Build(directory) -print " ---------------------------------------------------------- \n \n" -print " Installation of "+p_name+" finished without problems! \n \n" -print " Please read the README file in order to learn how to " -print " use the routines. \n \n" - + Build(directory) +print(" ---------------------------------------------------------- \n \n") +print(" Installation of "+p_name+" finished without problems! \n \n") +print(" Please read the README file in order to learn how to ") +print(" use the routines. \n \n") diff --git a/mike/mikepipe.py b/mike/mikepipe.py index 4c52c45..cd0e5af 100644 --- a/mike/mikepipe.py +++ b/mike/mikepipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * base = '../' @@ -86,7 +87,7 @@ ####### GLOBAL VARIABLES ##### ## perhaps put into options ## force_pre_process = False -force_flat = False +force_flat = False force_flat_extract = False force_thar_extract = False force_thar_wavcal = False @@ -109,7 +110,7 @@ NSigma_Marsh = 5 NCosmic_Marsh = 5 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 0 max_extract_col = int( 4102 / binx ) bias_sec = [1, int( 4102 / binx ),int( 4480 / binx )] @@ -126,190 +127,190 @@ ############################# log = dirout+'night.log' -print "\n\n\tMIKE Magellan6.5m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print "\tWill use only images with binning of", binning -print '\n' +print("\n\n\tMIKE Magellan6.5m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print("\tWill use only images with binning of", binning) +print('\n') biases, milkys, flatsR, flatsB, science, thars, thar_dates, obnames, exptimes = mikeutils.FileClassify(dirin,log,binning) nightlog = open(log,'r') loglines = nightlog.readlines() -print "\tThese are all the calibration images:" -print "\tMilky Flats:" +print("\tThese are all the calibration images:") +print("\tMilky Flats:") for milky in milkys: - h = pyfits.open(milky) - if milky[-2:] == 'fz': - h[0].header = h[1].header - print '\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], milky, h[0].header['UT-DATE'] -print "\tBlue Flats:" + h = pyfits.open(milky) + if milky[-2:] == 'fz': + h[0].header = h[1].header + print('\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], milky, h[0].header['UT-DATE']) +print("\tBlue Flats:") for flat in flatsB: - h = pyfits.open(flat) - if flat[-2:] == 'fz': - h[0].header = h[1].header - print '\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], flat, h[0].header['UT-DATE'] -print "\tRed Flats:" + h = pyfits.open(flat) + if flat[-2:] == 'fz': + h[0].header = h[1].header + print('\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], flat, h[0].header['UT-DATE']) +print("\tRed Flats:") for flat in flatsR: - h = pyfits.open(flat) - if flat[-2:] == 'fz': - h[0].header = h[1].header - print '\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], flat, h[0].header['UT-DATE'] -print "\tARCS:" + h = pyfits.open(flat) + if flat[-2:] == 'fz': + h[0].header = h[1].header + print('\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], flat, h[0].header['UT-DATE']) +print("\tARCS:") for thar in thars: - h = pyfits.open(thar) - if thar[-2:] == 'fz': - h[0].header = h[1].header - print '\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], thar, h[0].header['UT-DATE'] + h = pyfits.open(thar) + if thar[-2:] == 'fz': + h[0].header = h[1].header + print('\t\t', h[0].header['OBJECT'], h[0].header['EXPTIME'], thar, h[0].header['UT-DATE']) -print "\n\tThese are all the images to proccess:" +print("\n\tThese are all the images to proccess:") for line in loglines: - print '\t'+line[:-1] + print('\t'+line[:-1]) if stst == 'last': - if os.access(dirout+'findstar.txt',os.F_OK): - fst = open(dirout+'findstar.txt','r') - stst = fst.readline() - fst.close() - else: - raise ValueError("There is not a previously defined standard star file!!! You have to enter one (i.e. -ofind pfs0001.fits).\n") + if os.access(dirout+'findstar.txt',os.F_OK): + fst = open(dirout+'findstar.txt','r') + stst = fst.readline() + fst.close() + else: + raise ValueError("There is not a previously defined standard star file!!! You have to enter one (i.e. -ofind pfs0001.fits).\n") else: - fst = open(dirout+'findstar.txt','w') - fst.write(stst+'\n') - fst.close() + fst = open(dirout+'findstar.txt','w') + fst.write(stst+'\n') + fst.close() -if ( (os.access(dirout+'Flat.fits', os.F_OK) == False) or \ - (os.access(dirout+'trace.pkl', os.F_OK) == False) or \ +if ( (os.access(dirout+'Flat.fits', os.F_OK) == False) or \ + (os.access(dirout+'trace.pkl', os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if pre_process == 1: - # Find orders & traces - print "\tTracing echelle orders..." - - h = pyfits.open(dirin+stst) - if len(h) > 1: - h[0].data = h[1].data - d = h[0].data - d = mikeutils.OverscanTrim(d,bias_sec,ov_sec) - d = d.T - c_all, nord = GLOBALutils.get_them(d,ext_aperture,4,mode=1) - c_all = c_all[1:-1] - nord = len(c_all) - print '\t\t', nord, 'orders found in red CCD' - - hb = pyfits.open(dirin+'b'+stst[1:]) - if len(h) > 1: - hb[0].data = hb[1].data - db = hb[0].data - db = mikeutils.OverscanTrim(db,bias_sec,ov_sec) - db = db.T - c_all_b, nord_b = GLOBALutils.get_them(db,ext_aperture,4,mode=1) - print '\t\t', nord_b, 'orders found in blue CCD' - - # median combine list of ob flats - print "\tMedian combining flat frames" - if len(milkys) == 0: - print 'Warning: NO MILKY FLATS PROVIDED!' - Flat, RO_flat, GA_flat = mikeutils.MilkyCombine(milkys,bias_sec,ov_sec) - # save this file for later reference - hdu = pyfits.PrimaryHDU( Flat ) - if (os.access(dirout+'Flat.fits',os.F_OK)): - os.remove(dirout+'Flat.fits') - hdu.writeto(dirout+'Flat.fits') - - RFlat, RO_flatR, GA_flatR = mikeutils.FlatCombine(flatsR,bias_sec,ov_sec) - RFlat /= Flat - RFlat = RFlat.T - # save this file for later reference - hdu = pyfits.PrimaryHDU( RFlat ) - if (os.access(dirout+'FlatR.fits',os.F_OK)): - os.remove(dirout+'FlatR.fits') - hdu.writeto(dirout+'FlatR.fits') - - trace_dict = {'c_all':c_all, 'c_all_b':c_all_b, 'nord':nord, 'nord_b':nord_b, 'GA_flat': GA_flat, 'RO_flat': RO_flat} - pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) + # Find orders & traces + print("\tTracing echelle orders...") + + h = pyfits.open(dirin+stst) + if len(h) > 1: + h[0].data = h[1].data + d = h[0].data + d = mikeutils.OverscanTrim(d,bias_sec,ov_sec) + d = d.T + c_all, nord = GLOBALutils.get_them(d,ext_aperture,4,mode=1) + c_all = c_all[1:-1] + nord = len(c_all) + print('\t\t', nord, 'orders found in red CCD') + + hb = pyfits.open(dirin+'b'+stst[1:]) + if len(h) > 1: + hb[0].data = hb[1].data + db = hb[0].data + db = mikeutils.OverscanTrim(db,bias_sec,ov_sec) + db = db.T + c_all_b, nord_b = GLOBALutils.get_them(db,ext_aperture,4,mode=1) + print('\t\t', nord_b, 'orders found in blue CCD') + + # median combine list of ob flats + print("\tMedian combining flat frames") + if len(milkys) == 0: + print('Warning: NO MILKY FLATS PROVIDED!') + Flat, RO_flat, GA_flat = mikeutils.MilkyCombine(milkys,bias_sec,ov_sec) + # save this file for later reference + hdu = pyfits.PrimaryHDU( Flat ) + if (os.access(dirout+'Flat.fits',os.F_OK)): + os.remove(dirout+'Flat.fits') + hdu.writeto(dirout+'Flat.fits') + + RFlat, RO_flatR, GA_flatR = mikeutils.FlatCombine(flatsR,bias_sec,ov_sec) + RFlat /= Flat + RFlat = RFlat.T + # save this file for later reference + hdu = pyfits.PrimaryHDU( RFlat ) + if (os.access(dirout+'FlatR.fits',os.F_OK)): + os.remove(dirout+'FlatR.fits') + hdu.writeto(dirout+'FlatR.fits') + + trace_dict = {'c_all':c_all, 'c_all_b':c_all_b, 'nord':nord, 'nord_b':nord_b, 'GA_flat': GA_flat, 'RO_flat': RO_flat} + pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) else: - print '\tLoading Masterflat and traces' - trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) - c_all = trace_dict['c_all'] - c_all_b = trace_dict['c_all_b'] - nord = trace_dict['nord'] - nord_b = trace_dict['nord_b'] - GA_flat = trace_dict['GA_flat'] - RO_flat = trace_dict['RO_flat'] - Flat = pyfits.getdata(dirout+'Flat.fits') - RFlat = pyfits.getdata(dirout+'FlatR.fits') - -print '\n\tExtraction of Flat calibration frames:' + print('\tLoading Masterflat and traces') + trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) + c_all = trace_dict['c_all'] + c_all_b = trace_dict['c_all_b'] + nord = trace_dict['nord'] + nord_b = trace_dict['nord_b'] + GA_flat = trace_dict['GA_flat'] + RO_flat = trace_dict['RO_flat'] + Flat = pyfits.getdata(dirout+'Flat.fits') + RFlat = pyfits.getdata(dirout+'FlatR.fits') + +print('\n\tExtraction of Flat calibration frames:') Flat_spec_fits = dirout + 'Flat_spec.fits' Flat_bkg_fits = dirout + 'BKG_flat.fits' if ( os.access(Flat_spec_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo previous Flat extracted or extraction forced, extracting and saving..." - Centers = np.zeros((len(c_all),RFlat.shape[1])) - for i in range(nord): - Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) - BKG = GLOBALutils.get_scat(RFlat,Centers,span=10) - flat_S = GLOBALutils.simple_extraction( RFlat - BKG, c_all,ext_aperture, min_extract_col,max_extract_col, npools ) - flat_S = GLOBALutils.invert(flat_S) - flat_S = flat_S[::-1] - if (os.access(Flat_spec_fits,os.F_OK)): - os.remove( Flat_spec_fits ) - hdu = pyfits.PrimaryHDU( flat_S ) - hdu.writeto( Flat_spec_fits ) - if (os.access(Flat_bkg_fits,os.F_OK)): - os.remove( Flat_bkg_fits ) - hdu = pyfits.PrimaryHDU( BKG ) - hdu.writeto( Flat_bkg_fits ) -else: - print "\t\tExtracted flat found, loading..." - flat_S = pyfits.getdata( Flat_spec_fits ) + print("\t\tNo previous Flat extracted or extraction forced, extracting and saving...") + Centers = np.zeros((len(c_all),RFlat.shape[1])) + for i in range(nord): + Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) + BKG = GLOBALutils.get_scat(RFlat,Centers,span=10) + flat_S = GLOBALutils.simple_extraction( RFlat - BKG, c_all,ext_aperture, min_extract_col,max_extract_col, npools ) + flat_S = GLOBALutils.invert(flat_S) + flat_S = flat_S[::-1] + if (os.access(Flat_spec_fits,os.F_OK)): + os.remove( Flat_spec_fits ) + hdu = pyfits.PrimaryHDU( flat_S ) + hdu.writeto( Flat_spec_fits ) + if (os.access(Flat_bkg_fits,os.F_OK)): + os.remove( Flat_bkg_fits ) + hdu = pyfits.PrimaryHDU( BKG ) + hdu.writeto( Flat_bkg_fits ) +else: + print("\t\tExtracted flat found, loading...") + flat_S = pyfits.getdata( Flat_spec_fits ) flat_S_n, norms = GLOBALutils.FlatNormalize_single(flat_S, mid=int(.5*flat_S.shape[1])) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in thars: hthar = pyfits.open( fsim ) if len(hthar)>1: - ih = 1 + ih = 1 else: - ih = 0 + ih = 0 hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) thar_fits_simple = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.spec.simple.fits' if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): - dthar = mikeutils.OverscanTrim( hthar[ih].data, bias_sec,ov_sec ) - dthar /= Flat - dthar = dthar.T - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." - thar_Ss = GLOBALutils.simple_extraction(dthar, c_all,ext_aperture, min_extract_col,max_extract_col, npools ) - thar_Ss = GLOBALutils.invert(thar_Ss) - thar_Ss = thar_Ss[::-1] - + dthar = mikeutils.OverscanTrim( hthar[ih].data, bias_sec,ov_sec ) + dthar /= Flat + dthar = dthar.T + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") + thar_Ss = GLOBALutils.simple_extraction(dthar, c_all,ext_aperture, min_extract_col,max_extract_col, npools ) + thar_Ss = GLOBALutils.invert(thar_Ss) + thar_Ss = thar_Ss[::-1] + # save as fits file if (os.access(thar_fits_simple,os.F_OK)): - os.remove( thar_fits_simple ) + os.remove( thar_fits_simple ) hdu = pyfits.PrimaryHDU( thar_Ss ) hdu.writeto( thar_fits_simple ) else: - thar_Ss = pyfits.getdata(thar_fits_simple) + thar_Ss = pyfits.getdata(thar_fits_simple) """ maxxc = 0 for i in range(10,20,1): - ccf_max, shift = GLOBALutils.cor_thar(thar_Ss[i],span=100,filename=order_dir+'order_15.iwdat') - if ccf_max > maxxc: - maxxc = ccf_max - rough_shift = shift - or15 = i + ccf_max, shift = GLOBALutils.cor_thar(thar_Ss[i],span=100,filename=order_dir+'order_15.iwdat') + if ccf_max > maxxc: + maxxc = ccf_max + rough_shift = shift + or15 = i difo = 15 - or15 show() @@ -321,24 +322,24 @@ index = sorted_thar_dates[i] hthar = pyfits.open( thars[index] ) if len(hthar)>1: - ih = 1 + ih = 1 else: - ih = 0 + ih = 0 hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) thar_fits_simple = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.spec.simple.fits' wavsol_pkl = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour + '.wavsolpars.pkl' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on initial ThAr file", thars[index] - #print "\t\tExtracted ThAr file is", thar_fits_simple - + print "\t\tWorking on initial ThAr file", thars[index] + #print "\t\tExtracted ThAr file is", thar_fits_simple + mjd, mjd0 = mikeutils.mjd_fromheader( hthar ) thar_fits = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.spec.simple.fits' thar_S = pyfits.getdata( thar_fits ) - hd = pyfits.getheader(thar_fits) - thar_out = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.wav.fits' + hd = pyfits.getheader(thar_fits) + thar_out = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.wav.fits' - thar_data = np.zeros((2,thar_S.shape[0],thar_S.shape[1])) - thar_data[1]= thar_S.copy() + thar_data = np.zeros((2,thar_S.shape[0],thar_S.shape[1])) + thar_data[1]= thar_S.copy() lines_thar = thar_S.copy() @@ -348,26 +349,26 @@ All_Centroids = np.array([]) All_Sigmas = np.array([]) All_Intensities = np.array([]) - + for order in range(n_useful): order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar[order,:] - #bkg = PFSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = PFSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) thar_order = thar_order_orig - scipy.signal.medfilt(thar_order_orig,21) - wei = np.ones(len(thar_order)) - ccf_max, rshift = GLOBALutils.cor_thar(lines_thar[order],filename=order_dir+'order_'+\ - order_s+'.iwdat',span=20,binning=binx) + wei = np.ones(len(thar_order)) + ccf_max, rshift = GLOBALutils.cor_thar(lines_thar[order],filename=order_dir+'order_'+\ + order_s+'.iwdat',span=20,binning=binx) coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ - order_s+'.iwdat', thar_order, order, wei, rmsmax=100, \ - minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ - Dump_AllLines=True, Cheby=use_cheby,porder=5,del_width=4.0,\ - binning=binx,rough_shift=rshift,line_width=6) + rms_ms, residuals, centroids, sigmas, intensities =\ + GLOBALutils.Initial_Wav_Calibration(order_dir+'order_'+\ + order_s+'.iwdat', thar_order, order, wei, rmsmax=100, \ + minlines=30,FixEnds=False,Dump_Argon=dumpargon,\ + Dump_AllLines=True, Cheby=use_cheby,porder=5,del_width=4.0,\ + binning=binx,rough_shift=rshift,line_width=6) fwhms_lns = sigmas*2.355 inis_lns = pixel_centers - fwhms_lns*0.5 @@ -378,10 +379,10 @@ resolution = wavelengths / fwhms_wvs #print "\t\t\tmedian Resolution of order", order, '('+str(np.around(np.median(wavelengths)))+' A)=', np.around(np.median(resolution)) - #plot(wavelengths, resolution,'ro') - #show() - #plot(wavelengths,residuals,'o') - if (order == 16): + #plot(wavelengths, resolution,'ro') + #show() + #plot(wavelengths,residuals,'o') + if (order == 16): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order)) else: @@ -395,33 +396,33 @@ All_Intensities = np.append( All_Intensities, intensities ) p0 = np.zeros( npar_wsol ) - p0[0] = (16+37) * Global_ZP + p0[0] = (16+37) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ maxrms=150, Inv=Inverse_m,minlines=300,order0=37, \ ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - + # p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ # utils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ # np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ # maxrms=150, Inv=Inverse_m,minlines=300,nords=n_useful,length=len(thar_order)) - - if rms_ms/np.sqrt(float(len(G_wav))) < 10: - good_arcs.append(thars[index]) - equis = np.arange( thar_S.shape[1] ) - for order in range(n_useful): + if rms_ms/np.sqrt(float(len(G_wav))) < 10: + good_arcs.append(thars[index]) + + equis = np.arange( thar_S.shape[1] ) + for order in range(n_useful): m = order + 37 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=thar_S.shape[1], order0=37, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) + WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) thar_data[0,order,:] = WavSol - if (os.access(thar_out,os.F_OK)): + if (os.access(thar_out,os.F_OK)): os.remove( thar_out ) hdu = pyfits.PrimaryHDU( thar_data ) hdu.writeto( thar_out ) - + pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) @@ -429,14 +430,14 @@ else: print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl pdict = pickle.load(open(wavsol_pkl,'r')) - if pdict['rms_ms']/np.sqrt(float(len(pdict['G_wav']))) < 10: - good_arcs.append(thars[index]) + if pdict['rms_ms']/np.sqrt(float(len(pdict['G_wav']))) < 10: + good_arcs.append(thars[index]) good_arcs = np.array(good_arcs) hthar = pyfits.open(good_arcs[int(.5*len(good_arcs))]) ih = 0 if len(hthar)>1: - ih = 1 + ih = 1 hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) wavsolref_pkl = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour + '.wavsolpars.pkl' pdict = pickle.load(open(wavsolref_pkl,'r')) @@ -445,27 +446,27 @@ pshs = [] print "\n\tDetermination of Instrumental drift along the night:" for i in range(len(good_arcs)): - hthar = pyfits.open(good_arcs[i]) - hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) - thar_fits_simple = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.spec.simple.fits' - length = pyfits.getdata(thar_fits_simple).shape[1] - hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) - wavsol_pkl = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour + '.wavsolpars.pkl' - pdict = pickle.load(open(wavsol_pkl,'r')) - - p1, sh_centers, sh_orders, sh_wavelengths, sh_I, sh_rms_ms, sh_residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(pdict['G_pix'],\ - pdict['G_wav'], pdict['G_ord'],\ - np.ones(len(pdict['G_ord'])), pref,\ - minlines=250, maxrms=150,order0=37, ntotal=n_useful,\ - Cheby=use_cheby, Inv=Inverse_m, npix=length,nx=ncoef_x,nm=ncoef_m) - pshs.append(p1) - arc_mjds.append(pdict['mjd']) + hthar = pyfits.open(good_arcs[i]) + hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) + thar_fits_simple = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour +'.ThAr.spec.simple.fits' + length = pyfits.getdata(thar_fits_simple).shape[1] + hour = mikeutils.get_hour(float(hthar[ih].header['UT-TIME'])) + wavsol_pkl = dirout + 'MIKE_' + hthar[ih].header['UT-DATE'] + '_' + hour + '.wavsolpars.pkl' + pdict = pickle.load(open(wavsol_pkl,'r')) + + p1, sh_centers, sh_orders, sh_wavelengths, sh_I, sh_rms_ms, sh_residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(pdict['G_pix'],\ + pdict['G_wav'], pdict['G_ord'],\ + np.ones(len(pdict['G_ord'])), pref,\ + minlines=250, maxrms=150,order0=37, ntotal=n_useful,\ + Cheby=use_cheby, Inv=Inverse_m, npix=length,nx=ncoef_x,nm=ncoef_m) + pshs.append(p1) + arc_mjds.append(pdict['mjd']) pshs,arc_mjds = np.array(pshs),np.array(arc_mjds) tck_sh = scipy.interpolate.splrep(arc_mjds,pshs,k=1) """ ### start of science frame reductions ### -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') new_list = [] new_list_obnames = [] new_list_texp = [] @@ -474,13 +475,13 @@ obname = obnames[i] texp = exptimes[i] if (object2do == 'all'): - print '\t'+obname + print('\t'+obname) new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) else: if (obname == object2do): - print '\t'+obname + print('\t'+obname) new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) @@ -509,9 +510,9 @@ obname = new_list_obnames[ nlisti ] TEXP = new_list_texp[ nlisti ] - print '\n' - print "\t--> Working on image: ", fsim - print "\t\tObject name:",obname + print('\n') + print("\t--> Working on image: ", fsim) + print("\t\tObject name:",obname) know_moon = False if fsim.split('/')[-1] in spec_moon: @@ -522,12 +523,12 @@ h = pyfits.open(fsim) ih = 0 if len(h)>1: - ih = 1 - + ih = 1 + # get mjd and mjd0 mjd,mjd0 = mikeutils.mjd_fromheader(h) - # get gain and readnoise of object + # get gain and readnoise of object ronoise = h[ih].header['ENOISE'] gain = h[ih].header['EGAIN'] @@ -541,10 +542,10 @@ ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 + ra = ra2 + dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -557,15 +558,15 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name='Clay_Mag_2' - gobs.lat=rad(latitude) # lat/long in decimal degrees + gobs = ephem.Observer() + gobs.name='Clay_Mag_2' + gobs.lat=rad(latitude) # lat/long in decimal degrees gobs.long=rad(longitude) DDATE = h[ih].header['UT-DATE'] HHOUR = mikeutils.get_hour(float(h[ih].header['UT-TIME'])) @@ -575,14 +576,14 @@ gobs.date = str(DDATE[:4]) + '-' + str(DDATE[5:6]) + '-' + str(DDATE[7:]) + ' ' + Mho + ':' + Mmi +':' +Mse mephem = ephem.Moon() mephem.compute(gobs) - + Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sorted_indices = np.argsort( np.abs( np.array(thar_dates) - mjd ) ) @@ -597,74 +598,74 @@ data /= Flat data = data.T if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or (force_sci_extract): - - print '\t\t\tRecentering traces...' - c_alls, pshift = GLOBALutils.retrace( data, c_all ) - Centers = np.zeros((len(c_alls),data.shape[1])) - for i in range(nord): - Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) - - force_bkg = False - bkg_obj_fits = dirout + 'BKG_' + h[ih].header['UT-DATE'] + '_' + HHOUR +'.'+ obname +'.fits' - if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): - bkg = GLOBALutils.get_scat(data,Centers,span=1.8*ext_aperture) - if (os.access(bkg_obj_fits,os.F_OK)): - os.remove( bkg_obj_fits ) - hdu = pyfits.PrimaryHDU( bkg ) - hdu.writeto( bkg_obj_fits ) - else: - bkg = pyfits.getdata(bkg_obj_fits) - - data -= bkg - - force_P = False - if os.access(P_fits,os.F_OK) == False or force_P: - P = GLOBALutils.obtain_P(data,c_alls,ext_aperture,ronoise,\ - gain,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, min_extract_col,\ - max_extract_col, npools) - if (os.access(P_fits,os.F_OK)): - os.remove( P_fits ) - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( P_fits ) - else: - P = pyfits.getdata(P_fits) - - if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or (force_sci_extract): - - print "\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." - sci_Ss = GLOBALutils.simple_extraction(data,c_alls,ext_aperture,min_extract_col,max_extract_col,npools) - sci_S = GLOBALutils.optimal_extraction(data,P,c_alls,ext_aperture,ronoise,gain,S_Marsh,NCosmic_Marsh,\ - min_extract_col,max_extract_col,npools) - - sci_Ss = GLOBALutils.invert(sci_Ss)[::-1] - sci_S = GLOBALutils.invert(sci_S)[::-1] - - # save as fits file - if (os.access(sci_fits,os.F_OK)): - os.remove( sci_fits ) - if (os.access(sci_fits_simple,os.F_OK)): - os.remove( sci_fits_simple ) - - hdu = pyfits.PrimaryHDU( sci_S ) - hdu.writeto( sci_fits ) - hdu = pyfits.PrimaryHDU( sci_Ss ) - hdu.writeto( sci_fits_simple ) + + print('\t\t\tRecentering traces...') + c_alls, pshift = GLOBALutils.retrace( data, c_all ) + Centers = np.zeros((len(c_alls),data.shape[1])) + for i in range(nord): + Centers[i,:]=scipy.polyval(c_alls[i,:],np.arange(len(Centers[i,:]))) + + force_bkg = False + bkg_obj_fits = dirout + 'BKG_' + h[ih].header['UT-DATE'] + '_' + HHOUR +'.'+ obname +'.fits' + if ( os.access(bkg_obj_fits,os.F_OK) == False or force_bkg): + bkg = GLOBALutils.get_scat(data,Centers,span=1.8*ext_aperture) + if (os.access(bkg_obj_fits,os.F_OK)): + os.remove( bkg_obj_fits ) + hdu = pyfits.PrimaryHDU( bkg ) + hdu.writeto( bkg_obj_fits ) + else: + bkg = pyfits.getdata(bkg_obj_fits) + + data -= bkg + + force_P = False + if os.access(P_fits,os.F_OK) == False or force_P: + P = GLOBALutils.obtain_P(data,c_alls,ext_aperture,ronoise,\ + gain,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, min_extract_col,\ + max_extract_col, npools) + if (os.access(P_fits,os.F_OK)): + os.remove( P_fits ) + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( P_fits ) + else: + P = pyfits.getdata(P_fits) + + if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or (force_sci_extract): + + print("\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") + sci_Ss = GLOBALutils.simple_extraction(data,c_alls,ext_aperture,min_extract_col,max_extract_col,npools) + sci_S = GLOBALutils.optimal_extraction(data,P,c_alls,ext_aperture,ronoise,gain,S_Marsh,NCosmic_Marsh,\ + min_extract_col,max_extract_col,npools) + + sci_Ss = GLOBALutils.invert(sci_Ss)[::-1] + sci_S = GLOBALutils.invert(sci_S)[::-1] + + # save as fits file + if (os.access(sci_fits,os.F_OK)): + os.remove( sci_fits ) + if (os.access(sci_fits_simple,os.F_OK)): + os.remove( sci_fits_simple ) + + hdu = pyfits.PrimaryHDU( sci_S ) + hdu.writeto( sci_fits ) + hdu = pyfits.PrimaryHDU( sci_Ss ) + hdu.writeto( sci_fits_simple ) else: - print '\t\t'+fsim, "has already been extracted, reading in product fits files..." + print('\t\t'+fsim, "has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) - - + + fout = 'proc/'+ obname + '_' + \ h[ih].header['UT-DATE'] + '_' + \ HHOUR + '_' + 'sp.fits' #Build spectra - + if ( os.access(dirout+fout ,os.F_OK) == False ) or (force_spectral_file_build): - print '\t\tbuilding spectra ...' + print('\t\tbuilding spectra ...') # initialize file that will have the spectra spec = np.zeros((11, n_useful, data.shape[1])) hdu = pyfits.PrimaryHDU( spec ) @@ -677,11 +678,11 @@ #hdu = GLOBALutils.update_header(hdu,'HIERARCH EARTH ROTATION CORRECTION (km/s) [OBSOLETE]', gcvel) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) #hdu = GLOBALutils.update_header(hdu,'HIERARCH EARTH ROTATION CORRECTION (km/s)', vel_rot) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[ih].header['RA-D']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[ih].header['DEC-D']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',h[ih].header['EQUINOX']) hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',h[ih].header['SITELAT']) @@ -689,56 +690,56 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',h[ih].header['SITEALT']) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARG AIRMASS',h[ih].header['AIRMASS']) - psh = scipy.interpolate.splev(mjd,tck_sh) + psh = scipy.interpolate.splev(mjd,tck_sh) # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) - del_vels = [] + equis = np.arange( data.shape[1] ) + del_vels = [] for order in range(n_useful): m = order + 37 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1], order0=37, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) WavSol = GLOBALutils.ToVacuum(((1.+1.0e-6*psh)/float(m)) * GLOBALutils.Joint_Polynomial_Cheby(pref,chebs,ncoef_x,ncoef_m) * lbary_ltopo) spec[0,order,:] = WavSol spec[1,order,:] = sci_S[order,1, :] - if len( np.where(np.isnan(spec[1,order,:])) [0]) > 0: - spec[1,order,:] = sci_Ss[order,:] + if len( np.where(np.isnan(spec[1,order,:])) [0]) > 0: + spec[1,order,:] = sci_Ss[order,:] spec[2,order,:] = 1. / ( spec[1,order,:] / gain + (ronoise/gain)**2 ) - blaze = flat_S_n[order] #scipy.signal.medfilt(flat_S[order],21) - spec[3,order,:] = spec[1,order,:] / blaze - spec[4,order,:] = spec[2,order,:] * blaze ** 2 - #cont_coef = continuum.NORM_single(spec[0,order,:],scipy.signal.medfilt(spec[3,order,:],7),orden=3) - cont_coef = GLOBALutils.get_cont_single(spec[0,order],spec[3,order],spec[4,order],ll=1.5,lu=5,nc=3) - ratio = np.polyval(cont_coef, spec[0,order,:]) + blaze = flat_S_n[order] #scipy.signal.medfilt(flat_S[order],21) + spec[3,order,:] = spec[1,order,:] / blaze + spec[4,order,:] = spec[2,order,:] * blaze ** 2 + #cont_coef = continuum.NORM_single(spec[0,order,:],scipy.signal.medfilt(spec[3,order,:],7),orden=3) + cont_coef = GLOBALutils.get_cont_single(spec[0,order],spec[3,order],spec[4,order],ll=1.5,lu=5,nc=3) + ratio = np.polyval(cont_coef, spec[0,order,:]) L = np.where( spec[1,order,:] != 0 )[0] spec[5,order,:][L] = spec[3,order,:][L] / ratio[L] spec[6,order,:][L] = spec[4,order,:][L] * (ratio[L] ** 2 ) spec[7,order,:][L] = ratio[L] spec[8,order,:][L] = ratio[L] * blaze[L] / np.sqrt( ratio[L] * blaze[L] / gain + (ronoise/gain)**2 ) - rI = np.where(spec[5,order] > 1. + 8./spec[8,order]) - spec[5,order,rI] = 1. + rI = np.where(spec[5,order] > 1. + 8./spec[8,order]) + spec[5,order,rI] = 1. spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) dlambda_dx /= NN - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) #show() """ sky_shift,sky_rms,sky_err = milkyutils.get_sky_shift(spec,lbary_ltopo,order_dir+'sky_lines.dat') for order in range(n_useful): - I = np.where(spec[5,order] > 1 + 10./spec[8,order])[0] - #res = spec[5,order][nI] - medflx - #dev = np.sqrt(np.var(res)) - #I = np.where(spec[5,order] > 1. + 5*dev)[0] - spec[5,order][I]=1. - WavSol = spec[0,order,:] - spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) + I = np.where(spec[5,order] > 1 + 10./spec[8,order])[0] + #res = spec[5,order][nI] - medflx + #dev = np.sqrt(np.var(res)) + #I = np.where(spec[5,order] > 1. + 5*dev)[0] + spec[5,order][I]=1. + WavSol = spec[0,order,:] + spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), WavSol,k=3) dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) NN = np.average(dlambda_dx) dlambda_dx /= NN - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) @@ -751,226 +752,226 @@ hdu.writeto( dirout + fout ) """ if (not JustExtract): - if DoClass: - # spectral analysis - print "\t\tSpectral Analysis..." - query_success = False - # First, query SIMBAD with the object name - query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) - # Now, query SIMBAD by coordinates if above not successful - if (not query_success): - query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query - - hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) - - pars_file = dirout + 'MIKE_' + h[ih].header['UT-DATE'] + '_' + HHOUR +'.'+ obname +'_stellar_pars.txt' - if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" - T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec,model_path=models_path,npools=npools) - line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) - f = open(pars_file,'w') - f.write(line) - f.close() - - else: - print "\t\t\tAtmospheric parameters loaded from file:" - T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 - - else: - T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 - - T_eff_epoch = T_eff - logg_epoch = logg - Z_epoch = Z - vsini_epoch = vsini - vel0_epoch = vel0 - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - - medo = .5*spec.shape[2] - medd = .125*spec.shape[2] - SNR_5130 = np.median(spec[8,28,medo-medd:medo+medd+1] ) - - print "\t\tRadial Velocity analysis:" - # assign mask - sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - - velw = 300 - velsh = 3. - - # Read in mask - ml, mh, weight = np.loadtxt(mask,unpack=True) - ml_v = GLOBALutils.ToVacuum( ml ) - mh_v = GLOBALutils.ToVacuum( mh ) - - # make mask larger accounting for factor ~2.5 lower res in MIKE w/r to HARPS - av_m = 0.5*( ml_v + mh_v ) - ml_v -= 1.5*(av_m - ml_v) - mh_v += 1.5*(mh_v - av_m) - mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m - - disp = GLOBALutils.get_disp(obname, reffile=reffile) - if disp == 0: - known_sigma = False - if vsini != -999 and vsini != 0.: - disp = vsini - else: - disp = 3. + if DoClass: + # spectral analysis + print("\t\tSpectral Analysis...") + query_success = False + # First, query SIMBAD with the object name + query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) + # Now, query SIMBAD by coordinates if above not successful + if (not query_success): + query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) + + hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) + + pars_file = dirout + 'MIKE_' + h[ih].header['UT-DATE'] + '_' + HHOUR +'.'+ obname +'_stellar_pars.txt' + if os.access(pars_file,os.F_OK) == False or force_stellar_pars: + print("\t\t\tEstimating atmospheric parameters:") + T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec,model_path=models_path,npools=npools) + line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) + f = open(pars_file,'w') + f.write(line) + f.close() + else: + print("\t\t\tAtmospheric parameters loaded from file:") + T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) + + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) + + else: + T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + + T_eff_epoch = T_eff + logg_epoch = logg + Z_epoch = Z + vsini_epoch = vsini + vel0_epoch = vel0 + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) + + medo = .5*spec.shape[2] + medd = .125*spec.shape[2] + SNR_5130 = np.median(spec[8,28,medo-medd:medo+medd+1] ) + + print("\t\tRadial Velocity analysis:") + # assign mask + sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') + print("\t\t\tWill use",sp_type,"mask for CCF.") + + velw = 300 + velsh = 3. + + # Read in mask + ml, mh, weight = np.loadtxt(mask,unpack=True) + ml_v = GLOBALutils.ToVacuum( ml ) + mh_v = GLOBALutils.ToVacuum( mh ) + + # make mask larger accounting for factor ~2.5 lower res in MIKE w/r to HARPS + av_m = 0.5*( ml_v + mh_v ) + ml_v -= 1.5*(av_m - ml_v) + mh_v += 1.5*(mh_v - av_m) + mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m + + disp = GLOBALutils.get_disp(obname, reffile=reffile) + if disp == 0: + known_sigma = False + if vsini != -999 and vsini != 0.: + disp = vsini + else: + disp = 3. + else: + known_sigma = True + + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + + print('\t\t\tComputing the CCF...') + cond = True + while (cond): + # first rough correlation to find the minimum + vels, xc_full, sn, nlines_ccf, W_ccf = \ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=velw,vel_step=velsh,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) + yy = scipy.signal.medfilt(xc_av,11) + pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) + tck1 = scipy.interpolate.splrep(vels,pred,k=1) + xc_av_orig = xc_av.copy() + xc_av /= pred + vel0_xc = vels[ np.argmin( xc_av ) ] + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + xc_av_rough = xc_av + vels_rough = vels + + vel_width = np.maximum( 20.0, 6*disp ) + + vels, xc_full, sn, nlines_ccf, W_ccf =\ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.2,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) + pred = scipy.interpolate.splev(vels,tck1) + xc_av /= pred + + if sp_type == 'M5': + moon_sig = 2.5 + elif sp_type == 'K5': + moon_sig = 3.3 + else: + moon_sig = 4.5 + + p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) + + moonmatters = False + if (know_moon and here_moon): + moonmatters = True + ismoon = True + confused = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) + moon_flag = 1 + else: + confused = False + ismoon = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 + moon_flag = 0 + + bspan = GLOBALutils.calc_bss(vels,xc_av) + SP = bspan[0] + + #print p1gau[1] + if (not known_sigma): + disp = np.floor(p1gau[2]) + if (disp < 3.0): + disp = 3.0 + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True + else: + cond = False - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' - cond = True - while (cond): - # first rough correlation to find the minimum - vels, xc_full, sn, nlines_ccf, W_ccf = \ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, lbary_ltopo, vel_width=velw,vel_step=velsh,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) - yy = scipy.signal.medfilt(xc_av,11) - pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) - tck1 = scipy.interpolate.splrep(vels,pred,k=1) - xc_av_orig = xc_av.copy() - xc_av /= pred - vel0_xc = vels[ np.argmin( xc_av ) ] - rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc - xc_av_rough = xc_av - vels_rough = vels - - vel_width = np.maximum( 20.0, 6*disp ) - - vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.2,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) - pred = scipy.interpolate.splev(vels,tck1) - xc_av /= pred - - if sp_type == 'M5': - moon_sig = 2.5 - elif sp_type == 'K5': - moon_sig = 3.3 - else: - moon_sig = 4.5 - - p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) - - moonmatters = False - if (know_moon and here_moon): - moonmatters = True - ismoon = True - confused = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) - moon_flag = 1 - else: - confused = False - ismoon = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 - moon_flag = 0 - - bspan = GLOBALutils.calc_bss(vels,xc_av) - SP = bspan[0] - - #print p1gau[1] - if (not known_sigma): - disp = np.floor(p1gau[2]) - if (disp < 3.0): - disp = 3.0 - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - known_sigma = True - else: - cond = False + BSerr = -999.00 - BSerr = -999.00 + xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} - xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} - moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' + pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' - pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) + ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' - ccf_pdf = dirout + 'proc/' + fsim.split('/')[-1][:-4] + obname + '_XCs_' + sp_type + '.pdf' + if not avoid_plot: + GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - if not avoid_plot: - GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) + airmass = float(h[ih].header['AIRMASS']) + seeing = -999 - airmass = float(h[ih].header['AIRMASS']) - seeing = -999 - - if sp_type=='G2': - if T_eff < 6000: - D = 0.32815 - C = 0.00453 - else: - D = 0.32815 - C = 0.00453 - elif sp_type == 'K5': - D = 0.27404 - C = 0.00433 + if sp_type=='G2': + if T_eff < 6000: + D = 0.32815 + C = 0.00453 else: - D = 0.27404 - C = 0.00433 - BSerr = D / float(np.round(SNR_5130)) + C - RVerr = 0.5 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) - RVerr2 = np.around(RVerr,4) - BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) - - bjd_out = 2400000.5 + mbjd - T_eff_err = 100 - logg_err = 0.5 - Z_err = 0.5 - vsini_err = 2 - XC_min = np.abs(np.around(np.min(XCmodel),2)) - - SNR_5130 = np.around(SNR_5130) - SNR_5130_R = np.around(SNR_5130*np.sqrt(2.3)) - # write to output - disp_epoch = np.around(p1gau_m[2],1) - hdu = GLOBALutils.update_header(hdu,'RV', RV) - hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) - hdu = GLOBALutils.update_header(hdu,'BS', BS) - hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) - hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) - hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) - hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) - hdu = GLOBALutils.update_header(hdu,'INST', 'MIKE') - hdu = GLOBALutils.update_header(hdu,'RESOL', 50000) - hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') - hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) - hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - - line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f mike ceres 50000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ - (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) - f_res.write(line_out) - if (os.access( dirout + fout,os.F_OK)): - os.remove( dirout + fout) - hdu.writeto( dirout + fout ) - -f_res.close() \ No newline at end of file + D = 0.32815 + C = 0.00453 + elif sp_type == 'K5': + D = 0.27404 + C = 0.00433 + else: + D = 0.27404 + C = 0.00433 + BSerr = D / float(np.round(SNR_5130)) + C + RVerr = 0.5 + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) + RVerr2 = np.around(RVerr,4) + BSerr = np.around(BSerr,4) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) + + bjd_out = 2400000.5 + mbjd + T_eff_err = 100 + logg_err = 0.5 + Z_err = 0.5 + vsini_err = 2 + XC_min = np.abs(np.around(np.min(XCmodel),2)) + + SNR_5130 = np.around(SNR_5130) + SNR_5130_R = np.around(SNR_5130*np.sqrt(2.3)) + # write to output + disp_epoch = np.around(p1gau_m[2],1) + hdu = GLOBALutils.update_header(hdu,'RV', RV) + hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) + hdu = GLOBALutils.update_header(hdu,'BS', BS) + hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) + hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) + hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) + hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) + hdu = GLOBALutils.update_header(hdu,'INST', 'MIKE') + hdu = GLOBALutils.update_header(hdu,'RESOL', 50000) + hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') + hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) + hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) + + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f mike ceres 50000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ + (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ + TEXP, SNR_5130_R, ccf_pdf) + f_res.write(line_out) + if (os.access( dirout + fout,os.F_OK)): + os.remove( dirout + fout) + hdu.writeto( dirout + fout ) + +f_res.close() diff --git a/mike/mikeutils.py b/mike/mikeutils.py index 2f174b9..012d28c 100644 --- a/mike/mikeutils.py +++ b/mike/mikeutils.py @@ -1,5 +1,6 @@ +from __future__ import print_function import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from astropy.io import fits as pyfits import numpy as np import scipy @@ -20,17 +21,17 @@ def get_hour(hour): ss = int(((hour/3600. - hh) * 60. - mm)*60) shh,smm,sss = str(hh),str(mm),str(ss) if hh<10: - shh = '0'+shh + shh = '0'+shh if mm<10: - smm = '0'+smm + smm = '0'+smm if ss<10: - sss = '0'+sss + sss = '0'+sss hour = shh + ':' + smm + ':' + sss return hour def FileClassify(dir, log,binning): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -50,100 +51,100 @@ def FileClassify(dir, log,binning): bad_files = [] if os.access(dir+'bad_files.txt',os.F_OK): - bf = open(dir+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(dir+line[:-1]) - bf.close() + bf = open(dir+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(dir+line[:-1]) + bf.close() fix_files, ffEXP, ffOBJ = [],[],[] if os.access(dir+'fix_files.txt',os.F_OK): - ff = open(dir+'fix_files.txt') - linesff = ff.readlines() - for line in linesff: - cos = line.split() - fix_files.append(dir+cos[0]) - ffEXP.append(cos[1]) - ffOBJ.append(cos[2]) - ff.close() + ff = open(dir+'fix_files.txt') + linesff = ff.readlines() + for line in linesff: + cos = line.split() + fix_files.append(dir+cos[0]) + ffEXP.append(cos[1]) + ffOBJ.append(cos[2]) + ff.close() fix_files, ffEXP, ffOBJ = np.array(fix_files), np.array(ffEXP), np.array(ffOBJ) all_files = glob.glob(dir+"/r*fits*") for archivo in all_files: - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - h = pyfits.open(archivo) - if archivo[-2:] == 'fz': - h[0].header = h[1].header - - if dump == False and h[0].header['BINNING']==binning: - - I = np.where(archivo == fix_files)[0] - if len(I)>0: - h[0].header['EXPTYPE'] = ffEXP[I[0]] - h[0].header['OBJECT'] = ffOBJ[I[0]] - - if h[0].header['EXPTYPE'] == 'Object': - if 'milky' in str(h[0].header['OBJECT']).lower(): - milkys.append(archivo) - elif 'flat' in h[0].header['OBJECT'].lower(): - if 'blue' in h[0].header['OBJECT'].lower(): - flatsB.append(archivo) - else: - flatsR.append(archivo) - else: - science.append(archivo) - obname = h[0].header['OBJECT'] - ra = h[0].header['RA'] - delta = h[0].header['DEC'] - airmass = h[0].header['AIRMASS'] - texp = h[0].header['EXPTIME'] - date = h[0].header['UT-DATE'] - hour = float(h[0].header['UT-TIME']) - hh = int(hour/3600.) - mm = int((hour/3600. - hh) * 60.) - ss = int(((hour/3600. - hh) * 60. - mm)*60) - shh = str(hh) - smm = str(mm) - sss = str(ss) - if hh < 10: - shh = '0' + shh - if mm < 10: - smm = '0' + smm - if ss < 10: - sss = '0' + sss - hour = shh + ':' + smm + ':' + sss - obnames.append( obname ) - exptimes.append( texp ) - line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) - f.write(line) - elif h[0].header['EXPTYPE'] == 'Comp': - if 'milky' in h[0].header['OBJECT'].lower(): - milkys.append(archivo) - elif 'flat' in h[0].header['OBJECT'].lower(): - if 'blue' in h[0].header['OBJECT'].lower(): - flatsB.append(archivo) - else: - flatsR.append(archivo) - else: - thars.append(archivo) - mjd, mjd0 = mjd_fromheader(h) - thar_dates.append( mjd ) - elif h[0].header['EXPTYPE'] == 'Bias': - biases.append(archivo) - elif h[0].header['EXPTYPE'] == 'Flat': - if 'milky' in h[0].header['OBJECT'].lower(): - milkys.append(archivo) - else: - if 'blue' in h[0].header['OBJECT'].lower(): - flatsB.append(archivo) - else: - flatsR.append(archivo) - - + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + h = pyfits.open(archivo) + if archivo[-2:] == 'fz': + h[0].header = h[1].header + + if dump == False and h[0].header['BINNING']==binning: + + I = np.where(archivo == fix_files)[0] + if len(I)>0: + h[0].header['EXPTYPE'] = ffEXP[I[0]] + h[0].header['OBJECT'] = ffOBJ[I[0]] + + if h[0].header['EXPTYPE'] == 'Object': + if 'milky' in str(h[0].header['OBJECT']).lower(): + milkys.append(archivo) + elif 'flat' in h[0].header['OBJECT'].lower(): + if 'blue' in h[0].header['OBJECT'].lower(): + flatsB.append(archivo) + else: + flatsR.append(archivo) + else: + science.append(archivo) + obname = h[0].header['OBJECT'] + ra = h[0].header['RA'] + delta = h[0].header['DEC'] + airmass = h[0].header['AIRMASS'] + texp = h[0].header['EXPTIME'] + date = h[0].header['UT-DATE'] + hour = float(h[0].header['UT-TIME']) + hh = int(hour/3600.) + mm = int((hour/3600. - hh) * 60.) + ss = int(((hour/3600. - hh) * 60. - mm)*60) + shh = str(hh) + smm = str(mm) + sss = str(ss) + if hh < 10: + shh = '0' + shh + if mm < 10: + smm = '0' + smm + if ss < 10: + sss = '0' + sss + hour = shh + ':' + smm + ':' + sss + obnames.append( obname ) + exptimes.append( texp ) + line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) + f.write(line) + elif h[0].header['EXPTYPE'] == 'Comp': + if 'milky' in h[0].header['OBJECT'].lower(): + milkys.append(archivo) + elif 'flat' in h[0].header['OBJECT'].lower(): + if 'blue' in h[0].header['OBJECT'].lower(): + flatsB.append(archivo) + else: + flatsR.append(archivo) + else: + thars.append(archivo) + mjd, mjd0 = mjd_fromheader(h) + thar_dates.append( mjd ) + elif h[0].header['EXPTYPE'] == 'Bias': + biases.append(archivo) + elif h[0].header['EXPTYPE'] == 'Flat': + if 'milky' in h[0].header['OBJECT'].lower(): + milkys.append(archivo) + else: + if 'blue' in h[0].header['OBJECT'].lower(): + flatsB.append(archivo) + else: + flatsR.append(archivo) + + f.close() return biases, milkys, flatsR, flatsB, science, thars, thar_dates, obnames, exptimes @@ -154,8 +155,8 @@ def mjd_fromheader(h): """ ih = 0 if len(h)>1: - ih = 1 - datetu = h[ih].header['UT-DATE'] + ih = 1 + datetu = h[ih].header['UT-DATE'] ut = h[ih].header['UT-TIME'] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:])) ut = ut/3600. @@ -179,7 +180,7 @@ def MedianCombine(ImgList,bs,os, bias = 0.): d = h.data d = OverscanTrim(d,bs,os) d -= bias - + factor = 1.25 if (n < 3): factor = 1 @@ -187,7 +188,7 @@ def MedianCombine(ImgList,bs,os, bias = 0.): ronoise = factor * h.header['ENOISE'] / np.sqrt(n) gain = h.header['EGAIN'] - + if (n == 1): return d, ronoise, gain else: @@ -206,9 +207,9 @@ def MilkyCombine(ImgList,bs,os, bias = 0.): h = pyfits.open(ImgList[0]) if len(h)>1: - ih = 1 + ih = 1 else: - ih = 0 + ih = 0 d = h[ih].data d = OverscanTrim(d,bs,os) @@ -222,18 +223,18 @@ def MilkyCombine(ImgList,bs,os, bias = 0.): ronoise = factor * h[ih].header['ENOISE'] / np.sqrt(n) gain = h[ih].header['EGAIN'] - + if (n == 1): return d, ronoise, gain else: for i in range(n-1): h = pyfits.open(ImgList[i+1])[ih] - d2 = OverscanTrim(h.data,bs,os) - bias - md2 = signal.medfilt(d2,[21,5]) + d2 = OverscanTrim(h.data,bs,os) - bias + md2 = signal.medfilt(d2,[21,5]) d = np.dstack( (d, d2/md2.max() )) - d = np.median(d,axis=2) + d = np.median(d,axis=2) mm = signal.medfilt(d,[21,5]) - d /= mm + d /= mm return d, ronoise, gain def FlatCombine(ImgList,bs,os, bias = 0.): @@ -246,14 +247,14 @@ def FlatCombine(ImgList,bs,os, bias = 0.): h = pyfits.open(ImgList[0]) if len(h)>1: - ih = 1 + ih = 1 else: - ih = 0 + ih = 0 d = h[ih].data d = OverscanTrim(d,bs,os) d -= bias - + factor = 1.25 if (n < 3): factor = 1 @@ -261,16 +262,16 @@ def FlatCombine(ImgList,bs,os, bias = 0.): ronoise = factor * h[ih].header['ENOISE'] / np.sqrt(n) gain = h[ih].header['EGAIN'] - + if (n == 1): return d, ronoise, gain else: for i in range(n-1): h = pyfits.open(ImgList[i+1]) - d2 = OverscanTrim(h[ih].data,bs,os) - bias - md2 = signal.medfilt(d,[5,5]) + d2 = OverscanTrim(h[ih].data,bs,os) - bias + md2 = signal.medfilt(d,[5,5]) d = np.dstack( (d ,d2/(md2.max()) - bias )) - d = np.median(d,axis=2) + d = np.median(d,axis=2) return d, ronoise, gain def OverscanTrim(d,bs,os): @@ -280,12 +281,12 @@ def OverscanTrim(d,bs,os): bs[2] = d.shape[0] if bs[0] == 1: - ov = d[:,os[1]:os[2]] - ov = np.median(ov,axis = 1) - ov = np.tile(ov,os[1]) - ov = np.reshape(ov,[os[1],bs[2]]).T - newdata = d[:,:os[1]] - ov - newdata = newdata[:bs[1],:] + ov = d[:,os[1]:os[2]] + ov = np.median(ov,axis = 1) + ov = np.tile(ov,os[1]) + ov = np.reshape(ov,[os[1],bs[2]]).T + newdata = d[:,:os[1]] - ov + newdata = newdata[:bs[1],:] return newdata def make_blue(lista): @@ -298,15 +299,15 @@ def make_blue(lista): def get_sky_shift(spec,lbary_ltopo,path): lux = 2.99792458E8 def gauss_sky(params,x): - med = params[0] - sig = params[1] - amp = params[3] - bas = params[2] - g = bas + amp * np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) - return g + med = params[0] + sig = params[1] + amp = params[3] + bas = params[2] + g = bas + amp * np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) + return g def res_gauss_sky(params,g,x): - return g-gauss_sky(params,x) + return g-gauss_sky(params,x) sky_lines = np.loadtxt(path) sky_waves = sky_lines[:,0] @@ -317,78 +318,78 @@ def res_gauss_sky(params,g,x): del_vels = [] spec[0] /= lbary_ltopo for order in range(spec.shape[1]): - I = np.where((sky_waves>spec[0,order,10]) & (sky_wavesil - 1.) & (spec[0,order]= len(wavr): - break - else: - cond = False - x = spec[0,order,LIML:LIMH+1] - y = spec[5,order,LIML:LIMH+1] - else: - x = spec[0,order,LIML:LIMH+1] - y = spec[5,order,LIML:LIMH+1] - if len(med) == 1: - mm0 = med[0] - guess = [mm0,0.1,y.min(),(y-y.min()).max()] - Gfit = optimize.leastsq(res_gauss_sky,guess,args=(y,x)) - del_vel = lux * (Gfit[0][0] - mm0) / mm0 - del_vels.append(del_vel) - #print del_vel, med - elif len(med) == 2 and amp[0]==amp[1]: - mm0 = .5*(med[0]+med[1]) - guess = [mm0,0.1,y.min(),(y-y.min()).max()] - Gfit = optimize.leastsq(res_gauss_sky,guess,args=(y,x)) - del_vel = lux * (Gfit[0][0] - mm0) / mm0 - del_vels.append(del_vel) + I = np.where((sky_waves>spec[0,order,10]) & (sky_wavesil - 1.) & (spec[0,order]= len(wavr): + break + else: + cond = False + x = spec[0,order,LIML:LIMH+1] + y = spec[5,order,LIML:LIMH+1] + else: + x = spec[0,order,LIML:LIMH+1] + y = spec[5,order,LIML:LIMH+1] + if len(med) == 1: + mm0 = med[0] + guess = [mm0,0.1,y.min(),(y-y.min()).max()] + Gfit = optimize.leastsq(res_gauss_sky,guess,args=(y,x)) + del_vel = lux * (Gfit[0][0] - mm0) / mm0 + del_vels.append(del_vel) + #print del_vel, med + elif len(med) == 2 and amp[0]==amp[1]: + mm0 = .5*(med[0]+med[1]) + guess = [mm0,0.1,y.min(),(y-y.min()).max()] + Gfit = optimize.leastsq(res_gauss_sky,guess,args=(y,x)) + del_vel = lux * (Gfit[0][0] - mm0) / mm0 + del_vels.append(del_vel) del_vels = np.array(del_vels) - del_sky_ms, rms_sky_ms, ndel_vels = sig_cl_sky(del_vels) - print 'sky_shift =', del_sky_ms, rms_sky_ms, rms_sky_ms / np.sqrt(float(len(ndel_vels))) + del_sky_ms, rms_sky_ms, ndel_vels = sig_cl_sky(del_vels) + print('sky_shift =', del_sky_ms, rms_sky_ms, rms_sky_ms / np.sqrt(float(len(ndel_vels)))) #hist(ndel_vels,20) #show() return del_sky_ms, rms_sky_ms, rms_sky_ms / np.sqrt(float(len(ndel_vels))) def sig_cl_sky(vec): - m = np.median(vec) - res = vec - m - sig = np.sqrt( np.sum(res**2) / float(len(vec)-1) ) - cond = True - I = np.where(np.abs(res)>3*sig)[0] - if len(I) == 0 and sig < 2000: - cond = False - while cond: - iw = np.argmax(res**2) - vec = np.delete(vec,iw) - m = np.median(vec) - res = vec - m - sig = np.sqrt( np.sum(res**2) / float(len(vec)-1) ) - I = np.where(np.abs(res)>3*sig)[0] - if len(I) == 0 and sig < 2000: - cond = False - return m,sig,vec \ No newline at end of file + m = np.median(vec) + res = vec - m + sig = np.sqrt( np.sum(res**2) / float(len(vec)-1) ) + cond = True + I = np.where(np.abs(res)>3*sig)[0] + if len(I) == 0 and sig < 2000: + cond = False + while cond: + iw = np.argmax(res**2) + vec = np.delete(vec,iw) + m = np.median(vec) + res = vec - m + sig = np.sqrt( np.sum(res**2) / float(len(vec)-1) ) + I = np.where(np.abs(res)>3*sig)[0] + if len(I) == 0 and sig < 2000: + cond = False + return m,sig,vec diff --git a/pfs/pfspipe.py b/pfs/pfspipe.py index b24c306..b22f4c4 100644 --- a/pfs/pfspipe.py +++ b/pfs/pfspipe.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use("Agg") @@ -10,7 +11,7 @@ baryc_dir= base+'utils/SSEphem/' sys.path.append(baryc_dir) ephemeris='DEc403' - + import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages @@ -56,7 +57,7 @@ object2do = args.o2do reffile = args.reffile stst = args.ofind -binning = args.binning +binning = args.binning if dirin[-1] != '/': dirin = dirin + '/' @@ -102,7 +103,7 @@ NSigma_Marsh = 5 NCosmic_Marsh = 5 S_Marsh = 0.4 -N_Marsh = 3 # grado polinomio +N_Marsh = 3 # grado polinomio min_extract_col = 0 max_extract_col = 2032 bias_section = [1,2032,2160] @@ -122,7 +123,7 @@ lsnr1 = 1900 lsnr2 = 2101 fact = 2. - + ncoef_x = 6 ncoef_m = 7 @@ -136,18 +137,18 @@ log = dirout+'night.log' -print "\n\n\tPFS MAGELLAN6.5m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print "\tWill use only images with binning of", binning -print '\n' +print("\n\n\tPFS MAGELLAN6.5m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print("\tWill use only images with binning of", binning) +print('\n') biases, quartz, iodines, science, thars, thar_dates, obnames, exptimes = pfsutils.FileClassify(dirin,log,binning) nightlog = open(log,'r') loglines = nightlog.readlines() -print "\tThese are all the images to proccess:" +print("\tThese are all the images to proccess:") for line in loglines: - print '\t'+line[:-1] + print('\t'+line[:-1]) if stst == 'last': if os.access(dirout+'findstar.txt',os.F_OK): fst = open(dirout+'findstar.txt','r') @@ -164,26 +165,26 @@ (os.access(dirout+'MasterBias.fits', os.F_OK) == False) or \ (os.access(dirout+'trace.pkl', os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine Biases - print "\t\tGenerating Master calibration frames..." + print("\t\tGenerating Master calibration frames...") if len(biases)!=0: MasterBias, RO_bias, GA_bias = pfsutils.MedianCombine(biases, bias_section, over_section) else: MasterBias, RO_bias, GA_bias = np.zeros((max_extract_col,max_extract_col)),0,1 - print "\t\tWarning: 0 biases" + print("\t\tWarning: 0 biases") hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") #MasterBias = pyfits.getdata(dirout+'MasterBias.fits') #RO_bias, GA_bias = 1.,1. @@ -194,12 +195,12 @@ if (os.access(dirout+'Flat.fits',os.F_OK)): os.remove(dirout+'Flat.fits') hdu.writeto(dirout+'Flat.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") #Flat = pyfits.getdata(dirout+'Flat.fits') #RO_flat, GA_flat = 1.,1. - + # Find orders & traces - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") h = pyfits.open(dirin+stst)[0] hth = pyfits.getheader(dirin+stst) d = h.data @@ -212,7 +213,7 @@ else: - print '\t\tLoading Masterbias, Masterflat and traces' + print('\t\tLoading Masterbias, Masterflat and traces') trace_dict = pickle.load( open( dirout+'trace.pkl', 'r' ) ) c_all = trace_dict['c_all'] nord = trace_dict['nord'] @@ -225,18 +226,18 @@ Flat = h[0].data h = pyfits.open(dirout+'MasterBias.fits') MasterBias = h[0].data -print '\n' +print('\n') # Make flat NorFlat_fits = dirout + 'NorFlat.fits' force_flat = False if ( os.access(NorFlat_fits,os.F_OK) == False ) or (force_flat): - print "\tNo previous Normalized Flat computed or determination forced, computing and saving..." + print("\tNo previous Normalized Flat computed or determination forced, computing and saving...") NorFlat = np.ones(Flat.shape) eje = np.arange(Flat.shape[1]) medianFlat = scipy.signal.medfilt(Flat,[5,9]) #NorFlat = Flat/medianFlat - + vals = [] ffact = 1. if binning == '1x1': @@ -262,19 +263,19 @@ I = np.where(NorFlat==0) NorFlat[I] = 1. if (os.access(NorFlat_fits,os.F_OK)): - os.remove( NorFlat_fits ) + os.remove( NorFlat_fits ) hdu = pyfits.PrimaryHDU( NorFlat ) hdu.writeto( NorFlat_fits ) else: - print "\tNormalized Flat found, loading..." - NorFlat = pyfits.getdata( NorFlat_fits ) + print("\tNormalized Flat found, loading...") + NorFlat = pyfits.getdata( NorFlat_fits ) # Compute profile for optimal extraction P_fits = dirout + 'P.fits' if ( os.access(P_fits,os.F_OK) == False ) or (force_P_det): - print "\n\tNo previous P matrix determination or determination forced, computing and saving..." - + print("\n\tNo previous P matrix determination or determination forced, computing and saving...") + h = pyfits.open(dirin+stst)[0] hth = pyfits.getheader(dirin+stst) d = h.data @@ -307,36 +308,36 @@ # write P as fits file if (os.access(P_fits,os.F_OK)): - os.remove( P_fits ) + os.remove( P_fits ) hdu = pyfits.PrimaryHDU( P ) hdu.writeto( P_fits ) - + else: - print "\n\tP matrix found, loading..." + print("\n\tP matrix found, loading...") P = pyfits.getdata( P_fits ) # Extract Flat -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') Flat_spec_fits = dirout + 'Flat_spec.fits' if ( os.access(Flat_spec_fits,os.F_OK) == False ) or (force_flat_extract): - print "\t\tNo previous Flat extracted or extraction forced, extracting and saving..." + print("\t\tNo previous Flat extracted or extraction forced, extracting and saving...") flat_S = np.zeros( (nord,3,Flat.shape[1]) ) NFlat = Flat/NorFlat flat_S = GLOBALutils.simple_extraction(NFlat,c_all,ext_aperture,min_extract_col,max_extract_col,npools) #for i in range(nord): # flat_S[i,:,:] = getSpectrum( P, NFlat, c_all[i,:], ext_aperture, RO_flat, GA_flat, S_Marsh, 0*NCosmic_Marsh, min_extract_col,max_extract_col ) - + #flat_S = PFSutils.FlatNormalize( flat_S, mid = int(np.round(.5*flat_S.shape[2]))-1 ) if (os.access(Flat_spec_fits,os.F_OK)): os.remove( Flat_spec_fits ) hdu = pyfits.PrimaryHDU( flat_S ) hdu.writeto( Flat_spec_fits ) -else: - print "\t\tExtracted flat found, loading..." +else: + print("\t\tExtracted flat found, loading...") flat_S = pyfits.getdata( Flat_spec_fits ) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') # Extract all ThAr files for fsim in thars: hthar = pyfits.open( fsim )[0] @@ -347,18 +348,18 @@ thar_fits_simple = dirout + 'PFS_' + hthar.header['UT-DATE'] + '_' + hthar.header['UT-TIME'] +'.ThAr.spec.simple.fits' if ( os.access(thar_fits,os.F_OK) == False ) or ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,min_extract_col,max_extract_col,npools) thar_S = GLOBALutils.optimal_extraction(dthar,P,c_all,ext_aperture,hthar.header['ENOISE'],\ hthar.header['EGAIN'],S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) - + # save as fits file if (os.access(thar_fits,os.F_OK)): os.remove( thar_fits ) if (os.access(thar_fits_simple,os.F_OK)): os.remove( thar_fits_simple ) - + hdu = pyfits.PrimaryHDU( thar_S ) #hdu.header = hd hdu = GLOBALutils.update_header(hdu,'SLITNAME', hd['SLITNAME']) @@ -370,7 +371,7 @@ hdu.writeto( thar_fits_simple ) -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") # Compute wavelength calibration of ThAr sorted_thar_dates = np.argsort( thar_dates ) p0_array = np.zeros( (5, npar_wsol) ) @@ -380,8 +381,8 @@ hthar = pyfits.open( thars[index] ) wavsol_pkl = dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME']+'.wavsolpars.pkl' if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print "\t\tWorking on initial ThAr file", thars[index] - + print("\t\tWorking on initial ThAr file", thars[index]) + mjd, mjd0 = pfsutils.mjd_fromheader( hthar ) thar_fits = dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME'] +'.ThAr.spec.fits' thar_S = pyfits.getdata( thar_fits ) @@ -405,14 +406,14 @@ order_s = str(order) if (order < 10): order_s = '0'+str(order) - + thar_order_orig = lines_thar[order,:] IV = iv_thar[order,:] wei = np.sqrt( IV ) - #bkg = PFSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #bkg = PFSutils.Lines_mBack(thar_order_orig, IV, thres_rel=3) #thar_order = thar_order_orig - bkg thar_order = thar_order_orig - + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ rms_ms, residuals, centroids, sigmas, intensities =\ GLOBALutils.Initial_Wav_Calibration(order_dir+'norder_'+\ @@ -420,7 +421,7 @@ minlines=10,FixEnds=False,Dump_Argon=dumpargon,\ Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,fact=fact,del_width=30.) - if (order == 32): + if (order == 32): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order)) else: @@ -435,23 +436,23 @@ #show() p0 = np.zeros( npar_wsol ) - p0[0] = (32+93) * Global_ZP + p0[0] = (32+93) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ maxrms=50, Inv=Inverse_m,minlines=minlines_glob,order0=93, \ ntotal=n_useful,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - equis = np.arange( thar_S.shape[2] ) + equis = np.arange( thar_S.shape[2] ) for order in range(n_useful): m = order + 93 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=thar_S.shape[2], order0=93, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) + WavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) thar_data[0,order,:] = WavSol if (os.access(thar_out,os.F_OK)): os.remove( thar_out ) - + hdu = pyfits.PrimaryHDU( thar_data ) hdu = GLOBALutils.update_header(hdu,'SLITNAME', hd['SLITNAME']) hdu.writeto( thar_out ) @@ -460,20 +461,20 @@ # I = np.where(G_ord == orde)[0] # plot(G_wav[I],G_res[I],'o') #show() - + pdict = {'p1':p1,'mjd':mjd, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms,\ 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Orders':All_Orders, 'All_Sigmas':All_Sigmas} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) #print "\tMedian sigma:", np.median( All_Sigmas ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl,'r')) xc_fout_f = dirout+'ThAr_XCor+DeltaL.dat' force_tharxc = True -if ( (os.access(xc_fout_f,os.F_OK) == False) or (force_tharxc)): +if ( (os.access(xc_fout_f,os.F_OK) == False) or (force_tharxc)): xc_fout = open(xc_fout_f,'w') for i in range(len(sorted_thar_dates)): fsim = thars[sorted_thar_dates[i]] @@ -496,10 +497,10 @@ lines_thar = thar_S[:,1,:] lines_thar2 = thar_S2[:,1,:] - + pdict1 = pickle.load( open(dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME']+'.wavsolpars.pkl','r' ) ) pdict2 = pickle.load( open(dirout + 'PFS_' + hthar2[0].header['UT-DATE'] + '_' + hthar2[0].header['UT-TIME']+'.wavsolpars.pkl','r' ) ) - + # cross-correlate nob=n_useful out_shifts = np.zeros( nob ) @@ -507,20 +508,20 @@ for j in range(nob): XCor, shifts = GLOBALutils.XC_ThAr(lines_thar[j,:], lines_thar2[j,:], 20) p1 = GLOBALutils.XC_Gau_Fit(shifts, XCor, back_lag=2, usemin=0) - out_shifts[j] = p1[1] + out_shifts[j] = p1[1] # Now measure Delta \lambda m = j + 93 equis = np.arange(50,1950) chebs = GLOBALutils.Calculate_chebs(equis, m, npix=lines_thar.shape[1], order0=93, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol1 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(pdict1['p1'],chebs,ncoef_x,ncoef_m) - WavSol2 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(pdict2['p1'],chebs,ncoef_x,ncoef_m) + WavSol1 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(pdict1['p1'],chebs,ncoef_x,ncoef_m) + WavSol2 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(pdict2['p1'],chebs,ncoef_x,ncoef_m) out_dl[j] = np.mean( WavSol1 - WavSol2 ) * 299792458.0 / WavSol1[1000] rep = scipy.interpolate.splrep(equis,WavSol1) dldx = scipy.interpolate.splev(1000.0,rep,der=1) out_shifts[j] = p1[1] * dldx * 299792458.0 / WavSol1[1000] uno = np.median( out_shifts ) - dos = np.mean( out_dl ) + dos = np.mean( out_dl ) # write out products line_out = "%20.12f %20.12f %12.6f %14.8f %s %s\n" % (mjd, mjd2, uno, dos,thar_fits.split('/')[-1],thar_fits2.split('/')[-1]) @@ -528,7 +529,7 @@ #xc_fout.flush() xc_fout.close() -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') ### start of science frame reductions ### new_list = [] @@ -542,13 +543,13 @@ new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) - print '\t'+obname + print('\t'+obname) else: if (obname == object2do): new_list.append(fsim) new_list_obnames.append( obname ) new_list_texp.append( texp ) - print '\t'+obname + print('\t'+obname) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -569,15 +570,15 @@ spec_moon = np.array(spec_moon) use_moon = np.array(use_moon) -#new_list = new_list[1:] +#new_list = new_list[1:] for nlisti in range(len(new_list)): fsim = new_list[ nlisti ] obname = new_list_obnames[ nlisti ] TEXP = new_list_texp[ nlisti ] - print '\n' - print "\t--> Working on image: ", fsim - print "\t\tObject name:",obname + print('\n') + print("\t--> Working on image: ", fsim) + print("\t\tObject name:",obname) know_moon = False if fsim.split('/')[-1] in spec_moon: I = np.where(fsim.split('/')[-1] == spec_moon)[0] @@ -588,12 +589,12 @@ # get mjd and mjd0 mjd,mjd0 = pfsutils.mjd_fromheader(h) - # get gain and readnoise of object + # get gain and readnoise of object ronoise = h[0].header['ENOISE'] gain = h[0].header['EGAIN'] iodine = h[0].header['IODINE'] - - print "\t\tWorking on an observation of object",obname + + print("\t\tWorking on an observation of object",obname) # Find lambda_bary/lambda_topo using baryc altitude = h[0].header['SITEALT'] @@ -608,7 +609,7 @@ ra = ra2 dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) @@ -621,15 +622,15 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) - res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name='Clay_Mag_2' - gobs.lat=rad(latitude) # lat/long in decimal degrees + gobs = ephem.Observer() + gobs.name='Clay_Mag_2' + gobs.lat=rad(latitude) # lat/long in decimal degrees gobs.long=rad(longitude) DDATE = h[0].header['UT-DATE'] HHOUR = h[0].header['UT-TIME'] @@ -645,7 +646,7 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sorted_indices = np.argsort( np.abs( np.array(thar_dates) - mjd ) ) @@ -663,8 +664,8 @@ data -= MasterBias data /= NorFlat - - print '\t\t\tRecentering traces...' + + print('\t\t\tRecentering traces...') c_alls, pshift = GLOBALutils.retrace( data, c_all ) Centers = np.zeros((len(c_alls),data.shape[1])) @@ -699,35 +700,35 @@ if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or (force_sci_extract): - print "\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") sci_Ss = GLOBALutils.simple_extraction(data,c_alls,ext_aperture,min_extract_col,max_extract_col,npools) sci_S = GLOBALutils.optimal_extraction(data,P,c_alls,ext_aperture,ronoise,gain,S_Marsh,NCosmic_Marsh,\ min_extract_col,max_extract_col,npools) - + # save as fits file if (os.access(sci_fits,os.F_OK)): os.remove( sci_fits ) if (os.access(sci_fits_simple,os.F_OK)): os.remove( sci_fits_simple ) - + hdu = pyfits.PrimaryHDU( sci_S ) hdu.writeto( sci_fits ) hdu = pyfits.PrimaryHDU( sci_Ss ) hdu.writeto( sci_fits_simple ) else: - print '\t\t'+fsim, "has already been extracted, reading in product fits files..." + print('\t\t'+fsim, "has already been extracted, reading in product fits files...") sci_S = pyfits.getdata( sci_fits ) sci_Ss = pyfits.getdata( sci_fits_simple ) - - + + fout = 'proc/'+ obname + '_' + \ h[0].header['UT-DATE'] + '_' + \ h[0].header['UT-TIME'] + '_' + \ iodine + '_' + 'sp.fits' #Build spectra - + if ( os.access(dirout+fout ,os.F_OK) == False ) or (force_spectral_file_build): # initialize file that will have the spectra # n_useful should be nord_ob, but we still have not calibrated that bluest order -- TODO @@ -742,7 +743,7 @@ #hdu = GLOBALutils.update_header(hdu,'HIERARCH EARTH ROTATION CORRECTION (km/s) [OBSOLETE]', gcvel) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) #hdu = GLOBALutils.update_header(hdu,'HIERARCH EARTH ROTATION CORRECTION (km/s)', vel_rot) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_BARY)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_BARY)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA-D']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC-D']) @@ -759,16 +760,16 @@ hthar = pyfits.open(thars[indice]) thar_fits_ob = dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME'] +'.ThAr.spec.fits' pkl_wsol = dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME'] +'.wavsolpars.pkl' - print "\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + print("\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[1] ) + equis = np.arange( data.shape[1] ) for order in range(n_useful): m = order + 93 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[1], order0=93, ntotal=n_useful, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) + WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) spec[0,order,:] = GLOBALutils.ToVacuum(WavSol) spec[1,order,:] = sci_S[order,1, :] spec[2,order,:] = sci_S[order,2, :] @@ -791,7 +792,7 @@ NN = np.average(dlambda_dx) dlambda_dx /= NN - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) #show() @@ -803,24 +804,24 @@ ref_RES = 54000 elif h[0].header['SLITNAME'] == '0.3x2.5': ref_RES = 127000 - print ref_RES + print(ref_RES) if DoClass: # spectral analysis - print "\t\tSpectral Analysis..." + print("\t\tSpectral Analysis...") query_success = False # First, query SIMBAD with the object name query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + 'PFS_' + hthar[0].header['UT-DATE'] + '_' + hthar[0].header['UT-TIME'] +'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") spec2 = spec.copy() Rx = np.around(1./np.sqrt(1./40000.**2 - 1./ref_RES**2)) for i in range(spec.shape[1]): @@ -831,12 +832,12 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -853,19 +854,19 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - + print("\t\t\tWill use",sp_type,"mask for CCF.") + velw = 300 velsh = 3. - + # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2.5 lower res in FEROS w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= 1.*(av_m - ml_v) @@ -884,9 +885,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): # first rough correlation to find the minimum @@ -904,17 +905,17 @@ rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.2,\ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) - + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred - + if sp_type == 'M5': moon_sig = 2.5 elif sp_type == 'K5': @@ -943,11 +944,11 @@ #print p1gau[1] if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -977,7 +978,7 @@ airmass = h[0].header['AIRMASS'] seeing = -999 - + if sp_type=='G2': if T_eff < 6000: D = 0.32815 @@ -995,12 +996,12 @@ BSerr = D / float(np.round(SNR_5130)) + C RVerr = 0.5 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1033,4 +1034,4 @@ os.remove( dirout + fout) hdu.writeto( dirout + fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/pfs/pfsutils.py b/pfs/pfsutils.py index b5d4e37..571d202 100644 --- a/pfs/pfsutils.py +++ b/pfs/pfsutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use("Agg") from astropy.io import fits as pyfits @@ -38,7 +39,7 @@ def FileClassify(dir, log,binning): for line in linesbf: bad_files.append(dir+line[:-1]) bf.close() - + all_files = glob.glob(dir+"/pfs*fits") for archivo in all_files: @@ -47,7 +48,7 @@ def FileClassify(dir, log,binning): if archivo == bf: dump = True break - + h = pyfits.open(archivo) if dump == False and h[0].header['BINNING']==binning: if h[0].header['EXPTYPE'] == 'Object' and h[0].header['OBJECT'] != 'ThAr' and h[0].header['OBJECT'] != 'WideFlat': @@ -60,11 +61,11 @@ def FileClassify(dir, log,binning): date = h[0].header['UT-DATE'] hour = h[0].header['UT-TIME'] obnames.append( obname ) - exptimes.append( texp ) + exptimes.append( texp ) line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (obname, ra, delta, texp, airmass, date, hour, archivo) f.write(line) elif h[0].header['EXPTYPE'] == 'Bias': - print h[0].data.shape + print(h[0].data.shape) biases.append(archivo) elif h[0].header['EXPTYPE'] == 'Quartz' or h[0].header['EXPTYPE'] == 'Flat': if h[0].header['IODINE'] == 'Window': @@ -83,8 +84,8 @@ def mjd_fromheader(h): """ return modified Julian date from header """ - - datetu = h[0].header['UT-DATE'] + + datetu = h[0].header['UT-DATE'] ut = h[0].header['UT-TIME'] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:])) ut = float(ut[:2]) + float(ut[3:5])/60. + float(ut[6:])/3600. @@ -107,13 +108,13 @@ def MedianCombine(ImgList, bs, os, bias = 0.): #if n==0: # raise ValueError("empty list provided!") if n == 0: - print "\t\t\tWarning: 0 biases" + print("\t\t\tWarning: 0 biases") return 0, 0, 0 h = pyfits.open(ImgList[0])[0] d = h.data d = OverscanTrim(d,bs,os) d -= bias - + factor = 1.25 if (n < 3): factor = 1 @@ -121,7 +122,7 @@ def MedianCombine(ImgList, bs, os, bias = 0.): ronoise = factor * h.header['ENOISE'] / np.sqrt(n) gain = h.header['EGAIN'] - + if (n == 1): return d, ronoise, gain else: @@ -152,4 +153,4 @@ def OverscanTrim(d,bs,ts): bss = np.reshape(bss,[ts[1],bs[1]]).T newdata = d[:ts[1],:bs[1]] - bss - return newdata \ No newline at end of file + return newdata diff --git a/pucheros/pucherospipe.py b/pucheros/pucherospipe.py index 191d77d..f7aacd5 100644 --- a/pucheros/pucherospipe.py +++ b/pucheros/pucherospipe.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") from pylab import * base = '../' @@ -108,349 +109,349 @@ ncoef_m = 6 npar_wsol = (min(ncoef_x,ncoef_m) + 1) * (2*max(ncoef_x,ncoef_m) - min(ncoef_x,ncoef_m) + 2) / 2 -models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models +models_path = base+"data/COELHO_MODELS/R_40000b/" # path to the synthetic models order_dir = "wavcals/" n_useful = 41 # up to which order do we care? def gettime(hd): - date = hd['DATE-OBS'] - ye = float(date[:4]) - mo = float(date[5:7]) - da = float(date[8:10]) - ho = float(date[11:13]) - mi = float(date[14:16]) - se = float(date[17:]) - jd = pucherosutils.jd(ye,mo,da,ho,mi,se) + date = hd['DATE-OBS'] + ye = float(date[:4]) + mo = float(date[5:7]) + da = float(date[8:10]) + ho = float(date[11:13]) + mi = float(date[14:16]) + se = float(date[17:]) + jd = pucherosutils.jd(ye,mo,da,ho,mi,se) - return jd - 4./24. + return jd - 4./24. # file containing the log log = dirout+'night.log' -print "\n\n\tPUCHEROS ESO0.5m PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tPUCHEROS ESO0.5m PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # classify all the images according to its imagetype biases, flats, img_flats, fib_flats, objects, ThAr_ref, darks = pucherosutils.FileClassify(dirin,log) #print biases if len(flats) > 2: - have_flat = True - JustExtract = False + have_flat = True + JustExtract = False else: - print 'Warning: No sufficient fiber flats found ...' - have_flat = False - JustExtract = True + print('Warning: No sufficient fiber flats found ...') + have_flat = False + JustExtract = True have_darks = False if len(darks)>0: - have_darks = True + have_darks = True if len(fib_flats) > 0: - have_fib_flats = True + have_fib_flats = True else: - have_fib_flats = False + have_fib_flats = False if ( (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" - pre_process = 1 + print("\tNo previous pre-processing files or found") + pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" - pre_process = 0 + print("\tPre-processing files found, going straight to extraction") + pre_process = 0 RON,GAIN = pucherosutils.get_rg() if (pre_process == 1): - print "\tGenerating Master calibration frames..." - # median combine Biases - MasterBias, roB, gaB = pucherosutils.MedianCombine(biases, False, dirout+'MasterBias.fits') - hdu = pyfits.PrimaryHDU( MasterBias ) - if (os.access(dirout+'MasterBias.fits',os.F_OK)): - os.remove(dirout+'MasterBias.fits') - hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" - # median combine Darks - - if len(darks)!=0: - dark_times = [] - each_time = [] - - for dark in darks: - dh = pyfits.getheader(dark) - dtime = dh['EXPTIME'] - each_time.append(dtime) - esta = False - for t in dark_times: - if dtime == t: - esta = True - break - else: - esta = False - if esta == False: - dark_times.append(dtime) - - MasDarl = [] - - for t in dark_times: - sirven = [] - i = 0 - while i < len(darks): - if each_time[i] == t: - sirven.append(darks[i]) - i+=1 - Mdark, roD, gaF = pucherosutils.MedianCombine(sirven, True, dirout+'MasterBias.fits') - nMdark = 'MasterDark_'+str(t)+'s.fits' - if (os.access(dirout+nMdark,os.F_OK)): - os.remove(dirout+nMdark) - hdu = pyfits.PrimaryHDU( Mdark ) - hdu = GLOBALutils.update_header(hdu,'EXPTIME',t) - hdu.writeto(dirout+nMdark) - MasDarl.append(dirout+nMdark) - - darks_dict = {'d_names': MasDarl, 'd_times':dark_times} - pickle.dump( darks_dict, open( dirout+"darks.pkl", 'w' ) ) - print "\t\t-> Masterdarks: done!" - - else: - dark_times = [] - MasDarl = [] - darks_dict = {'d_names': MasDarl, 'd_times':dark_times} - pickle.dump( darks_dict, open( dirout+"darks.pkl", 'w' ) ) - print "\t\t-> 0 Masterdarks found!" - - # median combine list of flats - if len(flats)>0: - hd = pyfits.getdata(flats[0]) - Flat, roF, gaF = pucherosutils.MedianCombine(flats, True, dirout+'MasterBias.fits', dark_bo=have_darks, dlist=MasDarl) - hdu = pyfits.PrimaryHDU( Flat ) - if (os.access(dirout+'MasterFlat.fits',os.F_OK)): - os.remove(dirout+'MasterFlat.fits') - hdu.writeto(dirout+'MasterFlat.fits') - - print "\t\t-> Masterflats: done!" - print "\tTracing echelle orders..." - h = pyfits.open(dirout+'MasterFlat.fits')[0] - d = h.data - d = d.T - c_all, nord = GLOBALutils.get_them(d,ext_aperture+1,trace_degree,maxords=45) - print '\t\t'+str(nord)+' orders found ...' - trace_dict = {'c_all':c_all, 'nord':nord,'roF':roF,'gaF':gaF} - pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) + print("\tGenerating Master calibration frames...") + # median combine Biases + MasterBias, roB, gaB = pucherosutils.MedianCombine(biases, False, dirout+'MasterBias.fits') + hdu = pyfits.PrimaryHDU( MasterBias ) + if (os.access(dirout+'MasterBias.fits',os.F_OK)): + os.remove(dirout+'MasterBias.fits') + hdu.writeto(dirout+'MasterBias.fits') + print("\t\t-> Masterbias: done!") + # median combine Darks + + if len(darks)!=0: + dark_times = [] + each_time = [] + + for dark in darks: + dh = pyfits.getheader(dark) + dtime = dh['EXPTIME'] + each_time.append(dtime) + esta = False + for t in dark_times: + if dtime == t: + esta = True + break + else: + esta = False + if esta == False: + dark_times.append(dtime) + + MasDarl = [] + + for t in dark_times: + sirven = [] + i = 0 + while i < len(darks): + if each_time[i] == t: + sirven.append(darks[i]) + i+=1 + Mdark, roD, gaF = pucherosutils.MedianCombine(sirven, True, dirout+'MasterBias.fits') + nMdark = 'MasterDark_'+str(t)+'s.fits' + if (os.access(dirout+nMdark,os.F_OK)): + os.remove(dirout+nMdark) + hdu = pyfits.PrimaryHDU( Mdark ) + hdu = GLOBALutils.update_header(hdu,'EXPTIME',t) + hdu.writeto(dirout+nMdark) + MasDarl.append(dirout+nMdark) + + darks_dict = {'d_names': MasDarl, 'd_times':dark_times} + pickle.dump( darks_dict, open( dirout+"darks.pkl", 'w' ) ) + print("\t\t-> Masterdarks: done!") + + else: + dark_times = [] + MasDarl = [] + darks_dict = {'d_names': MasDarl, 'd_times':dark_times} + pickle.dump( darks_dict, open( dirout+"darks.pkl", 'w' ) ) + print("\t\t-> 0 Masterdarks found!") + + # median combine list of flats + if len(flats)>0: + hd = pyfits.getdata(flats[0]) + Flat, roF, gaF = pucherosutils.MedianCombine(flats, True, dirout+'MasterBias.fits', dark_bo=have_darks, dlist=MasDarl) + hdu = pyfits.PrimaryHDU( Flat ) + if (os.access(dirout+'MasterFlat.fits',os.F_OK)): + os.remove(dirout+'MasterFlat.fits') + hdu.writeto(dirout+'MasterFlat.fits') + + print("\t\t-> Masterflats: done!") + print("\tTracing echelle orders...") + h = pyfits.open(dirout+'MasterFlat.fits')[0] + d = h.data + d = d.T + c_all, nord = GLOBALutils.get_them(d,ext_aperture+1,trace_degree,maxords=45) + print('\t\t'+str(nord)+' orders found ...') + trace_dict = {'c_all':c_all, 'nord':nord,'roF':roF,'gaF':gaF} + pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) else: - h = pyfits.open(dirout+'MasterBias.fits') - MasterBias = h[0].data - # load orders & tracers - trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) - #print trace_dict['c_all'].shape - c_all = trace_dict['c_all'] - nord = trace_dict['nord'] - roF = trace_dict['roF'] - gaF = trace_dict['gaF'] - darks_dict = pickle.load( open( dirout+"darks.pkl", 'r' ) ) - dark_times = darks_dict['d_times'] - MasDarl = darks_dict['d_names'] - h = pyfits.open(dirout+'MasterFlat.fits')[0] - d = h.data - d = d.T - Flat = d.copy() + h = pyfits.open(dirout+'MasterBias.fits') + MasterBias = h[0].data + # load orders & tracers + trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) ) + #print trace_dict['c_all'].shape + c_all = trace_dict['c_all'] + nord = trace_dict['nord'] + roF = trace_dict['roF'] + gaF = trace_dict['gaF'] + darks_dict = pickle.load( open( dirout+"darks.pkl", 'r' ) ) + dark_times = darks_dict['d_times'] + MasDarl = darks_dict['d_names'] + h = pyfits.open(dirout+'MasterFlat.fits')[0] + d = h.data + d = d.T + Flat = d.copy() #MasterBias = 0. ################################ ThAr spectra extraction & calibration ################################################## thtimes = [] nThAr_ref = [] nthtimes = [] -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') #force_thar_extract = True for thar in ThAr_ref: - #print thar - h = pyfits.open(thar) - mjd,mjd0 = pucherosutils.mjd_fromheader(h) - hdth = pyfits.getheader(thar) - thtime = mjd - thtimes.append( thtime ) - - dthar = pyfits.getdata( thar ) - MasterBias - dthar = dthar.T - - #force_thar_extract=False - thar_fits_simple = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.simple.fits.S' - - if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): - - print "\t\tNo previous extraction or extraction forced for ThAr file", thar, "extracting..." - thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,\ - 0,1023,npools) - thar_Ss = thar_Ss[::-1] - - # save as fits file - if (os.access(thar_fits_simple,os.F_OK)): - os.remove( thar_fits_simple ) - - hdu = pyfits.PrimaryHDU( thar_Ss ) - hdu.writeto( thar_fits_simple ) - - else: - print "\t\tThAr file", thar, "all ready extracted, loading..." - thar_Ss = pyfits.getdata(thar_fits_simple) + #print thar + h = pyfits.open(thar) + mjd,mjd0 = pucherosutils.mjd_fromheader(h) + hdth = pyfits.getheader(thar) + thtime = mjd + thtimes.append( thtime ) + + dthar = pyfits.getdata( thar ) - MasterBias + dthar = dthar.T + + #force_thar_extract=False + thar_fits_simple = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.simple.fits.S' + + if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): + + print("\t\tNo previous extraction or extraction forced for ThAr file", thar, "extracting...") + thar_Ss = GLOBALutils.simple_extraction(dthar,c_all,ext_aperture,\ + 0,1023,npools) + thar_Ss = thar_Ss[::-1] + + # save as fits file + if (os.access(thar_fits_simple,os.F_OK)): + os.remove( thar_fits_simple ) + + hdu = pyfits.PrimaryHDU( thar_Ss ) + hdu.writeto( thar_fits_simple ) + + else: + print("\t\tThAr file", thar, "all ready extracted, loading...") + thar_Ss = pyfits.getdata(thar_fits_simple) numt = 0 -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") for thar in ThAr_ref: - h = pyfits.open(thar) - mjd,mjd0 = pucherosutils.mjd_fromheader(h) - hdth = pyfits.getheader(thar) - thtime = mjd - thar_fits_simple = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.simple.fits.S' - thar_fits_simple_wav = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.wav.fits.S' - wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - - #force_thar_wavcal = True - if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - thar_Ss = pyfits.getdata(thar_fits_simple) - print "\t\tWorking on ThAr file", thar - - lines_thar = thar_Ss.copy() - force_ofind=True - if numt == 0: - if os.access(dirout+'order_find.pkl',os.F_OK)==False or force_ofind: - maxes = 0 - or14 = 0 - for order in range(len(lines_thar)): - ccf_max, deltar = GLOBALutils.cor_thar(lines_thar[order],span=15,filename=order_dir+'gorder14.dat') - #print order, ccf_max - if ccf_max > maxes: - maxes = ccf_max - or14 = order - delta = deltar - - or0 = or14 - 14 - or40 = or14 + 26 - #print or14 - if or0 >= 0: - orwa = 0 - else: - orwa = - or0 - or0 = 0 - - if or40 <= nord - 1: - orwb = 40 - else: - orwb = 40 - (or40 - nord - 1) - or40 = nord - 1 - #print or0,or40, orwa, orwb - - pdict = {'orwa':orwa, 'or0':or0, 'orwb':orwb, 'or40':or40, 'delta':delta} - pickle.dump( pdict, open( dirout+'order_find.pkl', 'w' ) ) - else: - pdict = pickle.load(open(dirout+'order_find.pkl','r')) - orwa = pdict['orwa'] - or0 = pdict['or0'] - orwb = pdict['orwb'] - or40 = pdict['or40'] - delta = pdict['delta'] - - iv_thar = 1/((lines_thar/GAIN) + (RON**2/GAIN**2)) - - All_Pixel_Centers = np.array([]) - All_Wavelengths = np.array([]) - All_Orders = np.array([]) - All_Centroids = np.array([]) - All_Sigmas = np.array([]) - All_Intensities = np.array([]) - All_Residuals = np.array([]) - All_Sigmas = np.array([]) - orre = or0 - order = orwa - - OK = [] - OW = [] - nup = or40 - or0 + 1 - trans = np.zeros([nup,4]) - while orre <= or40: - order_s = str(order) - if (order < 10): - order_s = '0'+str(order) - - thar_order_orig = lines_thar[orre,:] - IV = iv_thar[orre,:] - wei = np.sqrt( IV ) - #bkg = utils.Lines_mBack(thar_order_orig, IV, thres_rel=3) - #thar_order = thar_order_orig - bkg - thar_order = thar_order_orig - coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ - rms_ms, residuals, centroids, sigmas, intensities \ - = GLOBALutils.Initial_Wav_Calibration(order_dir+'gorder'+order_s+'.dat',\ - thar_order,order,wei,rmsmax=5000000,\ - minlines=6,FixEnds=True,Dump_Argon=False,\ - Dump_AllLines=True, Cheby=use_cheby,rough_shift=delta) - - if (order == 20): - if (use_cheby): - Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 511, len(thar_order) ) - else: - Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) - #print residuals - All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) - All_Wavelengths = np.append( All_Wavelengths, wavelengths ) - All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) - All_Centroids = np.append( All_Centroids, centroids) - All_Sigmas = np.append( All_Sigmas, sigmas) - All_Intensities = np.append( All_Intensities, intensities ) - All_Residuals = np.append( All_Residuals, residuals) - All_Sigmas = np.append( All_Sigmas,sigmas) - trans[orre,:] = coeffs_pix2wav - - order += 1 - orre += 1 - """ - JJ = np.unique(All_Orders) - for order in JJ: - I = np.where(All_Orders == order)[0] - plot(All_Wavelengths[I],All_Residuals[I],'o') - show() - """ - p0 = np.zeros( npar_wsol ) - oro0 = 58 - p0[0] = (20+oro0) * Global_ZP - p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ - np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ - maxrms=150, Inv=Inverse_m,minlines=250,order0=oro0, \ - ntotal=nup,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) - - #print p1 - #for odd in np.unique(G_ord): - # Io = np.where(G_ord == odd)[0] - # plot(G_wav[Io], G_res[Io],'.') - #show() - #print gfd - thar_wav_Ss = np.zeros( (2,nup,dthar.shape[1]) ) - equis = np.arange( np.shape(thar_wav_Ss)[2] ) - order = orwa - orre = 0 - - while orre < nup: - m = order + oro0 - chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0,ntotal=nup, npix=len(thar_order), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - thar_wav_Ss[0,orre,:] = GLOBALutils.ToVacuum( (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) )[::-1] - thar_wav_Ss[1,orre,:] = thar_Ss[orre][::-1] - orre += 1 - order+=1 - - if (os.access(thar_fits_simple_wav,os.F_OK)): - os.remove( thar_fits_simple_wav ) - - hdu = pyfits.PrimaryHDU( thar_wav_Ss ) - hdu.writeto( thar_fits_simple_wav ) - - #if rms_ms/np.sqrt(NL)<50: - nThAr_ref.append(thar) - nthtimes.append(thtime) - pdict = {'p1':p1, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms, 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Sigmas':All_Sigmas, 'trans':trans, 'or0':or0, 'orwa':orwa, 'oro0':oro0} - pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) - numt+=1 + h = pyfits.open(thar) + mjd,mjd0 = pucherosutils.mjd_fromheader(h) + hdth = pyfits.getheader(thar) + thtime = mjd + thar_fits_simple = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.simple.fits.S' + thar_fits_simple_wav = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.spec.wav.fits.S' + wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + + #force_thar_wavcal = True + if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): + thar_Ss = pyfits.getdata(thar_fits_simple) + print("\t\tWorking on ThAr file", thar) + + lines_thar = thar_Ss.copy() + force_ofind=True + if numt == 0: + if os.access(dirout+'order_find.pkl',os.F_OK)==False or force_ofind: + maxes = 0 + or14 = 0 + for order in range(len(lines_thar)): + ccf_max, deltar = GLOBALutils.cor_thar(lines_thar[order],span=15,filename=order_dir+'gorder14.dat') + #print order, ccf_max + if ccf_max > maxes: + maxes = ccf_max + or14 = order + delta = deltar + + or0 = or14 - 14 + or40 = or14 + 26 + #print or14 + if or0 >= 0: + orwa = 0 + else: + orwa = - or0 + or0 = 0 + + if or40 <= nord - 1: + orwb = 40 + else: + orwb = 40 - (or40 - nord - 1) + or40 = nord - 1 + #print or0,or40, orwa, orwb + + pdict = {'orwa':orwa, 'or0':or0, 'orwb':orwb, 'or40':or40, 'delta':delta} + pickle.dump( pdict, open( dirout+'order_find.pkl', 'w' ) ) + else: + pdict = pickle.load(open(dirout+'order_find.pkl','r')) + orwa = pdict['orwa'] + or0 = pdict['or0'] + orwb = pdict['orwb'] + or40 = pdict['or40'] + delta = pdict['delta'] + + iv_thar = 1/((lines_thar/GAIN) + (RON**2/GAIN**2)) + + All_Pixel_Centers = np.array([]) + All_Wavelengths = np.array([]) + All_Orders = np.array([]) + All_Centroids = np.array([]) + All_Sigmas = np.array([]) + All_Intensities = np.array([]) + All_Residuals = np.array([]) + All_Sigmas = np.array([]) + orre = or0 + order = orwa + + OK = [] + OW = [] + nup = or40 - or0 + 1 + trans = np.zeros([nup,4]) + while orre <= or40: + order_s = str(order) + if (order < 10): + order_s = '0'+str(order) + + thar_order_orig = lines_thar[orre,:] + IV = iv_thar[orre,:] + wei = np.sqrt( IV ) + #bkg = utils.Lines_mBack(thar_order_orig, IV, thres_rel=3) + #thar_order = thar_order_orig - bkg + thar_order = thar_order_orig + coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ + rms_ms, residuals, centroids, sigmas, intensities \ + = GLOBALutils.Initial_Wav_Calibration(order_dir+'gorder'+order_s+'.dat',\ + thar_order,order,wei,rmsmax=5000000,\ + minlines=6,FixEnds=True,Dump_Argon=False,\ + Dump_AllLines=True, Cheby=use_cheby,rough_shift=delta) + + if (order == 20): + if (use_cheby): + Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 511, len(thar_order) ) + else: + Global_ZP = scipy.polyval( coeffs_pix2wav, 0.0 ) + #print residuals + All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) + All_Wavelengths = np.append( All_Wavelengths, wavelengths ) + All_Orders = np.append( All_Orders, np.zeros( len(pixel_centers) ) + order ) + All_Centroids = np.append( All_Centroids, centroids) + All_Sigmas = np.append( All_Sigmas, sigmas) + All_Intensities = np.append( All_Intensities, intensities ) + All_Residuals = np.append( All_Residuals, residuals) + All_Sigmas = np.append( All_Sigmas,sigmas) + trans[orre,:] = coeffs_pix2wav + + order += 1 + orre += 1 + """ + JJ = np.unique(All_Orders) + for order in JJ: + I = np.where(All_Orders == order)[0] + plot(All_Wavelengths[I],All_Residuals[I],'o') + show() + """ + p0 = np.zeros( npar_wsol ) + oro0 = 58 + p0[0] = (20+oro0) * Global_ZP + p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ + np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ + maxrms=150, Inv=Inverse_m,minlines=250,order0=oro0, \ + ntotal=nup,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) + + #print p1 + #for odd in np.unique(G_ord): + # Io = np.where(G_ord == odd)[0] + # plot(G_wav[Io], G_res[Io],'.') + #show() + #print gfd + thar_wav_Ss = np.zeros( (2,nup,dthar.shape[1]) ) + equis = np.arange( np.shape(thar_wav_Ss)[2] ) + order = orwa + orre = 0 + + while orre < nup: + m = order + oro0 + chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0,ntotal=nup, npix=len(thar_order), Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + thar_wav_Ss[0,orre,:] = GLOBALutils.ToVacuum( (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(p1,chebs,ncoef_x,ncoef_m) )[::-1] + thar_wav_Ss[1,orre,:] = thar_Ss[orre][::-1] + orre += 1 + order+=1 + + if (os.access(thar_fits_simple_wav,os.F_OK)): + os.remove( thar_fits_simple_wav ) + + hdu = pyfits.PrimaryHDU( thar_wav_Ss ) + hdu.writeto( thar_fits_simple_wav ) + + #if rms_ms/np.sqrt(NL)<50: + nThAr_ref.append(thar) + nthtimes.append(thtime) + pdict = {'p1':p1, 'G_pix':G_pix, 'G_ord':G_ord, 'G_wav':G_wav, 'II':II, 'rms_ms':rms_ms, 'G_res':G_res, 'All_Centroids':All_Centroids, 'All_Sigmas':All_Sigmas, 'trans':trans, 'or0':or0, 'orwa':orwa, 'oro0':oro0} + pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) + numt+=1 pdict = pickle.load(open(dirout+'order_find.pkl','r')) orwa = pdict['orwa'] @@ -472,12 +473,12 @@ def gettime(hd): min_rms = 100000 thar_min = ThAr_ref[0] for thar in ThAr_ref: - hdth = pyfits.getheader(thar) - wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - wavsol = pickle.load( open( wavsol_pkl, 'r' ) ) - if wavsol['rms_ms'] < min_rms: - min_rms = wavsol['rms_ms'] - thar_min = thar + hdth = pyfits.getheader(thar) + wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + wavsol = pickle.load( open( wavsol_pkl, 'r' ) ) + if wavsol['rms_ms'] < min_rms: + min_rms = wavsol['rms_ms'] + thar_min = thar thar_min = ThAr_ref[0] hdth = pyfits.getheader(thar_min) thtime = gettime(hdth) @@ -489,116 +490,116 @@ def gettime(hd): thshifts = [] thtimes = [] for thar in ThAr_ref: - h = pyfits.open(thar) - mjd,mjd0 = pucherosutils.mjd_fromheader(h) - hdth = pyfits.getheader(thar) - thtimes.append(mjd) - wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - wavsol = pickle.load( open( wavsol_pkl, 'r' ) ) - G_pix = wavsol['G_pix'] - G_wav = wavsol['G_wav'] - G_ord = wavsol['G_ord'] - oro0 = wavsol['oro0'] - p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ - GLOBALutils.Global_Wav_Solution_vel_shift(G_pix, G_wav, G_ord,\ - np.ones(G_wav.shape), p1_ref, Cheby=True, Inv=True, \ - maxrms=150,minlines=250,order0=oro0,ntotal=nup,\ - npix=h[0].data.shape[0],nx=ncoef_x,nm=ncoef_m) - thshifts.append(p_shift) - f.write(thar+'\t'+str(thtime)+'\t'+str((1e-6*p_shift)*299792.458)+'\n') - #print p_shift + h = pyfits.open(thar) + mjd,mjd0 = pucherosutils.mjd_fromheader(h) + hdth = pyfits.getheader(thar) + thtimes.append(mjd) + wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + wavsol = pickle.load( open( wavsol_pkl, 'r' ) ) + G_pix = wavsol['G_pix'] + G_wav = wavsol['G_wav'] + G_ord = wavsol['G_ord'] + oro0 = wavsol['oro0'] + p_shift, pix_centers, orders, wavelengths, I, rms_ms, residuals = \ + GLOBALutils.Global_Wav_Solution_vel_shift(G_pix, G_wav, G_ord,\ + np.ones(G_wav.shape), p1_ref, Cheby=True, Inv=True, \ + maxrms=150,minlines=250,order0=oro0,ntotal=nup,\ + npix=h[0].data.shape[0],nx=ncoef_x,nm=ncoef_m) + thshifts.append(p_shift) + f.write(thar+'\t'+str(thtime)+'\t'+str((1e-6*p_shift)*299792.458)+'\n') + #print p_shift thshifts,thtimes = np.array(thshifts),np.array(thtimes) Is = np.argsort(thtimes) thtimes,thshifts = thtimes[Is],thshifts[Is] if len(thtimes) > 3: - thshifts = (1e-6*thshifts)*299792.458 - thtck = scipy.interpolate.splrep(thtimes,thshifts,k=3,s=0) - ejeje = np.arange(thtimes[0],thtimes[-1],0.0001) - ejeyy = scipy.interpolate.splev(ejeje,thtck,der=0) - #if not avoid_plot: - # plot(thtimes-56413,thshifts,'ro') - # plot(ejeje-56413,ejeyy) - # show() + thshifts = (1e-6*thshifts)*299792.458 + thtck = scipy.interpolate.splrep(thtimes,thshifts,k=3,s=0) + ejeje = np.arange(thtimes[0],thtimes[-1],0.0001) + ejeyy = scipy.interpolate.splev(ejeje,thtck,der=0) + #if not avoid_plot: + # plot(thtimes-56413,thshifts,'ro') + # plot(ejeje-56413,ejeyy) + # show() else: - thtck = 0 + thtck = 0 f.close() #print ThAr_ref c_all = trace_dict['c_all'][::-1] c_all = c_all[or0:or40+1] c_all = c_all[::-1] -print '\n\tExtraction of Flat calibration frames:' +print('\n\tExtraction of Flat calibration frames:') if have_flat: - # names of extracted Masterflat (optimal & simple) & of the P flat matrix - S_flat_fits = dirout+'Masterflat.spec.fits' - S_flat_fits_simple = dirout+'Masterflat.spec.fits.S' - flat_P = dirout+'P_Flat.fits' - sm_flat_fits = dirout+'SmoothMasterflat.spec.fits' - # name of the extracted background image - bacfile = dirout+'BkgFlatImage_Flat.fits' - if ( os.access(S_flat_fits,os.F_OK) == False ) or ( os.access(S_flat_fits_simple,os.F_OK) == False ) or (force_flat_extract) or ( os.access(flat_P,os.F_OK) == False ): - - print "\t\tNo previous extraction or extraction forced for flat file", "extracting..." - #Flat = utils.invert(Flat) - - Flat = pyfits.getdata(dirout+'MasterFlat.fits') - Flat = Flat.T - if (os.access(bacfile,os.F_OK))== False or True: - Centers = np.zeros((len(c_all),Flat.shape[0])) - for i in range(c_all.shape[0]): - Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) - bac = GLOBALutils.get_scat(Flat,Centers,span=5) - hdbac = pyfits.PrimaryHDU( bac ) - if os.access(bacfile,os.F_OK): - os.system('rm -r '+bacfile) - hdbac.writeto(bacfile) - Flat -= bac - - # Determination the P matrix - if os.access(flat_P,os.F_OK): - P = pyfits.getdata(flat_P) - else: - P = GLOBALutils.obtain_P(Flat,c_all,ext_aperture,roF,\ - gaF,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, 0,1023, npools) - hdu = pyfits.PrimaryHDU(P) - hdu.writeto(flat_P) - - flat_S = GLOBALutils.optimal_extraction(Flat,P,c_all,ext_aperture,roF,\ - gaF,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) - sm_flat = flat_S.copy() - flat_Ss = GLOBALutils.simple_extraction(Flat,c_,ext_aperture,\ - min_extract_col,max_extract_col,npools) - - flat_S = flat_S[::-1] - flat_Ss = flat_Ss[::-1] - sm_flat = sm_flat[::-1] - # save as fits file - if (os.access(S_flat_fits,os.F_OK)): - os.remove( S_flat_fits ) - if (os.access(S_flat_fits_simple,os.F_OK)): - os.remove( S_flat_fits_simple ) - if (os.access(flat_P,os.F_OK)): - os.remove( flat_P ) - if (os.access(sm_flat_fits,os.F_OK)): - os.remove( sm_flat_fits ) - - hdu = pyfits.PrimaryHDU( flat_S ) - hdu.writeto( S_flat_fits ) - hdu = pyfits.PrimaryHDU( flat_Ss ) - hdu.writeto( S_flat_fits_simple ) - hdu = pyfits.PrimaryHDU( P ) - hdu.writeto( flat_P ) - hdu = pyfits.PrimaryHDU( sm_flat ) - hdu.writeto( sm_flat_fits ) - - # recover Flat spectra and P matrix - else: - flat_S = pyfits.getdata(S_flat_fits) - flat_Ss = pyfits.getdata(S_flat_fits_simple) - P = pyfits.getdata(flat_P) - sm_flat = pyfits.getdata( sm_flat_fits ) + # names of extracted Masterflat (optimal & simple) & of the P flat matrix + S_flat_fits = dirout+'Masterflat.spec.fits' + S_flat_fits_simple = dirout+'Masterflat.spec.fits.S' + flat_P = dirout+'P_Flat.fits' + sm_flat_fits = dirout+'SmoothMasterflat.spec.fits' + # name of the extracted background image + bacfile = dirout+'BkgFlatImage_Flat.fits' + if ( os.access(S_flat_fits,os.F_OK) == False ) or ( os.access(S_flat_fits_simple,os.F_OK) == False ) or (force_flat_extract) or ( os.access(flat_P,os.F_OK) == False ): + + print("\t\tNo previous extraction or extraction forced for flat file", "extracting...") + #Flat = utils.invert(Flat) + + Flat = pyfits.getdata(dirout+'MasterFlat.fits') + Flat = Flat.T + if (os.access(bacfile,os.F_OK))== False or True: + Centers = np.zeros((len(c_all),Flat.shape[0])) + for i in range(c_all.shape[0]): + Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) + bac = GLOBALutils.get_scat(Flat,Centers,span=5) + hdbac = pyfits.PrimaryHDU( bac ) + if os.access(bacfile,os.F_OK): + os.system('rm -r '+bacfile) + hdbac.writeto(bacfile) + Flat -= bac + + # Determination the P matrix + if os.access(flat_P,os.F_OK): + P = pyfits.getdata(flat_P) + else: + P = GLOBALutils.obtain_P(Flat,c_all,ext_aperture,roF,\ + gaF,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, 0,1023, npools) + hdu = pyfits.PrimaryHDU(P) + hdu.writeto(flat_P) + + flat_S = GLOBALutils.optimal_extraction(Flat,P,c_all,ext_aperture,roF,\ + gaF,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) + sm_flat = flat_S.copy() + flat_Ss = GLOBALutils.simple_extraction(Flat,c_,ext_aperture,\ + min_extract_col,max_extract_col,npools) + + flat_S = flat_S[::-1] + flat_Ss = flat_Ss[::-1] + sm_flat = sm_flat[::-1] + # save as fits file + if (os.access(S_flat_fits,os.F_OK)): + os.remove( S_flat_fits ) + if (os.access(S_flat_fits_simple,os.F_OK)): + os.remove( S_flat_fits_simple ) + if (os.access(flat_P,os.F_OK)): + os.remove( flat_P ) + if (os.access(sm_flat_fits,os.F_OK)): + os.remove( sm_flat_fits ) + + hdu = pyfits.PrimaryHDU( flat_S ) + hdu.writeto( S_flat_fits ) + hdu = pyfits.PrimaryHDU( flat_Ss ) + hdu.writeto( S_flat_fits_simple ) + hdu = pyfits.PrimaryHDU( P ) + hdu.writeto( flat_P ) + hdu = pyfits.PrimaryHDU( sm_flat ) + hdu.writeto( sm_flat_fits ) + + # recover Flat spectra and P matrix + else: + flat_S = pyfits.getdata(S_flat_fits) + flat_Ss = pyfits.getdata(S_flat_fits_simple) + P = pyfits.getdata(flat_P) + sm_flat = pyfits.getdata( sm_flat_fits ) ##################################################### Science images extraction ###################################################################### sm_flat, norms = GLOBALutils.FlatNormalize_single( sm_flat[:,1,:], mid=int(0.5*sm_flat.shape[2]),span=200) @@ -617,9 +618,9 @@ def gettime(hd): new_list.append(fsim) new_list_obnames.append( obname ) -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') for nlisti in range(len(new_list)): - print '\t\t'+new_list_obnames[nlisti] + print('\t\t'+new_list_obnames[nlisti]) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -641,545 +642,545 @@ def gettime(hd): fin_spec = [] have_specph = False for obj in new_list: - - know_moon = False - if obj.split('/')[-1] in spec_moon: - I = np.where(obj.split('/')[-1] == spec_moon)[0] - know_moon = True - here_moon = use_moon[I] - - print "\t--> Working on image: ", obj - index1,index2 = -1,-1 - h = pyfits.open(obj) - mjd,mjd0 = pucherosutils.mjd_fromheader(h) - hd = pyfits.getheader(obj) - nombre = pucherosutils.search_name(obj) - exptime = hd['EXPTIME'] - - print "\t\tObject name:",nombre - - altitude = 1450. - latitude = -(33. + 16./60. + 9./3600.) - longitude = -(70. + 32./60. + 4./3600.) - known_coords = False - sp,ra,dec,known_coords = pucherosutils.get_coords(nombre,mjd) - epoch = 2000. - - ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) - if ra2 !=0 and dec2 != 0: - ra = ra2 - dec = dec2 - known_coords = True - elif known_coords: - print '\t\tUsing the coordinates from symbad query.' - else: - print '\t\tUnknown coordinate for this object.' - - if known_coords: - iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) - obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) - obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) - jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) - jplephem.set_observer_coordinates( obpos[0], obpos[1], obpos[2] ) - res = jplephem.doppler_fraction(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) - lbary_ltopo = 1.0 + res['frac'][0] - bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc - res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) - mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) - - else: - bcvel_baryc = 0. - mbjd = mjd - - # Moon Phase Calculations - gobs = ephem.Observer() - gobs.name = 'ODUC' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) - - gobs.date = hd['DATE-OBS'].replace('T',' ') - - mephem = ephem.Moon() - mephem.compute(gobs) - Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) - Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) - res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) - lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) - refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel - - - #print utils.search_name(obj) - nama = pucherosutils.search_name(obj)+'_'+hd['DATE-OBS'][:10]+'_'+hd['DATE-OBS'][11:13]+'-'+hd['DATE-OBS'][14:16]+'-'+hd['DATE-OBS'][17:] - obj_fits = dirout+nama+'.spec.fits.S' - bkg_obj_fits = dirout+'Bkg_'+nama+'.fits' - obj_fits_simple = dirout+nama+'.spec.simple.fits.S' - - - if ( os.access(obj_fits,os.F_OK) == False ) or \ - ( os.access(obj_fits_simple,os.F_OK) == False ) or \ - (force_sci_extract): - print "\t\tNo previous extraction or extraction forced for science file", obj, "extracting..." - - dat = pyfits.getdata(obj).astype('float') - dat -= MasterBias - if len(MasDarl)>0 and dark_corr: - dat -= pucherosutils.get_dark(MasDarl, hd['EXPTIME']) - dat = dat.T - - drift, c_new = GLOBALutils.get_drift(dat,P,c_all,pii=512,win=5) - print '\t\ty drift:', drift - - P_new = GLOBALutils.shift_P(P,drift,c_new,ext_aperture) - - if back_corr: - Centers = np.zeros((len(c_new),dat.shape[1])) - for i in range(c_new.shape[0]): - Centers[i,:]=scipy.polyval(c_new[i,:],np.arange(len(Centers[i,:]))) - bac = GLOBALutils.get_scat(dat, Centers,span=5,allow_neg=True) - dat -= bac - - if not have_flat: - P_new = GLOBALutils.obtain_P(dat,c_new,ext_aperture,RON,\ - GAIN,NSigma_Marsh, S_Marsh, \ - N_Marsh, Marsh_alg, 0,1023, npools) - obj_Ss = GLOBALutils.simple_extraction(dat,c_new,ext_aperture,0,1023,npools) - obj_S = GLOBALutils.optimal_extraction(dat,P_new,c_new,ext_aperture,\ - RON,GAIN,S_Marsh,NCosmic_Marsh,0,1023,npools) - - obj_Ss = obj_Ss[::-1] - obj_S = obj_S[::-1] - - # save as fits file - if (os.access(obj_fits,os.F_OK)): - os.remove( obj_fits ) - if (os.access(obj_fits_simple,os.F_OK)): - os.remove( obj_fits_simple ) - - hdu = pyfits.PrimaryHDU( obj_S ) - hdu.writeto( obj_fits ) - hdu = pyfits.PrimaryHDU( obj_Ss ) - hdu.writeto( obj_fits_simple ) - - else: - obj_S = pyfits.getdata(obj_fits) - obj_Ss = pyfits.getdata(obj_fits_simple) - - deltat=1000 - i = 0 - while i < len(thtimes): - if abs(thtimes[i]-mjd) < deltat: - index1 = i - deltat = abs(thtimes[i]-mjd) - i+=1 - - - if mjd < thtimes[0]: - print "Problem with ThAr and science times" - index1 = 0 - index2 = 0 - indexx = 0 - elif mjd > thtimes[-1]: - print "Problem with ThAr and science times" - index1 = -1 - index2 = -1 - indexx = -1 - else: - for i in range(len(thtimes)-1): - if mjd >= thtimes[i] and mjd < thtimes[i+1]: - index1 = i - index2 = i+1 - if abs(mjd - thtimes[i]) < abs(mjd - thtimes[i+1]): - indexx = i - else: - indexx = i+1 - break - - #print ThAr_ref[index1], obj, ThAr_ref[index2] - hdth = pyfits.getheader(ThAr_ref[index1]) - wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - pdict = pickle.load(open(wavsol_pkl,'r')) - global1 = pdict['p1'] - All_Pixel_Centers = pdict['G_pix'] - All_Orders = pdict['G_ord'] - All_Wavelengths = pdict['G_wav'] - rms_ms = pdict['rms_ms'] - All_Residuals = pdict['G_res'] - All_Centroids = pdict['All_Centroids'] - All_Sigmas = pdict['All_Sigmas'] - trans = pdict['trans'] - or0 = pdict['or0'] - orwa = pdict['orwa'] - oro0 = pdict['oro0'] - - hdth = pyfits.getheader(ThAr_ref[index2]) - wavsol_pkl2 = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - pdict2 = pickle.load(open(wavsol_pkl2,'r')) - global2 = pdict2['p1'] - - hdth = pyfits.getheader(ThAr_ref[indexx]) - wavsol_pklx = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' - pdictx = pickle.load(open(wavsol_pklx,'r')) - globalx = pdictx['p1'] - - if JustExtract: - final = np.zeros( [6, nup,np.shape(obj_S)[2]] ) - else: - final = np.zeros( [11, nup,np.shape(obj_S)[2]] ) - hdu = pyfits.PrimaryHDU( final ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', hdth['DATE-OBS'][:10] ) - hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', hdth['DATE-OBS'][:11]) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hdth['EXPTIME']) - hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY) / LAMBDA_TOPO)', lbary_ltopo) - hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', nombre) - try: - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',ra) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',dec) - hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) - except: - None - hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) - hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) - - equis = np.arange( np.shape(obj_S)[2] ) - order = orwa - orre = 0 - - while orre < nup: - - m = order + oro0 - chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0, npix=len(equis), ntotal=nup, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol1 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global1,chebs,ncoef_x,ncoef_m) - WavSol2 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global2,chebs,ncoef_x,ncoef_m) - WavSolx = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(globalx,chebs,ncoef_x,ncoef_m) - bestWavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(best_p1,chebs,ncoef_x,ncoef_m) - - if thtimes[index2] == thtimes[index1]: - WavSol = WavSol1 - else: - pen = (WavSol2 - WavSol1) / (thtimes[index2] - thtimes[index1]) - coe = WavSol2 - pen* thtimes[index2] - WavSol = pen*mjd + coe - if thtck == 0.: - p_shift = 0. - else: - p_shift = scipy.interpolate.splev(mjd, thtck, der=0) - p_shift = 0 - WavSol = bestWavSol * (1.0 + 1.0e-6*p_shift) - final[0,orre,:] = GLOBALutils.ToVacuum(WavSol[::-1]) - - #final[0,orre,:] = WavSolx[::-1] - if len(np.where(np.isnan(obj_S[orre,2,:][::-1]) == True) [0]) < 100: - final[1,orre,:] = obj_S[orre,1,:][::-1] # ...flux in ADU (optimal extraction)... - final[2,orre,:] = obj_S[orre,2,:][::-1] # ...and 1/variance... - if JustExtract==False: - final[3,orre,:] = final[1,orre,:] / sm_flat[orre,:][::-1] # ...flux (optimal)/flat... - final[4,orre,:] = final[2,orre,:] * (sm_flat[orre,:][::-1] ** 2) # ...and 1/variance * flat**2... - cont = GLOBALutils.get_cont_single(final[0,orre,:],final[3,orre,:],final[4,orre,:], nc = 3) - ratio = np.polyval(cont,final[0,orre]) - final[6,orre,:] = final[4,orre,:] * (ratio**2) - final[7,orre,:] = ratio - final[8,orre,:] = ratio * sm_flat[orre,:][::-1] / np.sqrt( ratio * sm_flat[orre,:][::-1] / GAIN + (RON/GAIN)**2 ) - final[5,orre,:] = final[3,orre,:] / ratio - nJ = np.where(np.isnan(final[5,orre])==True)[0] - nJ2 = np.where(np.isinf(final[5,orre])==True)[0] - final[5,orre,nJ] = 1.0 - final[5,orre,nJ2] = 1.0 - #plot(final[8,orre]) - rI = np.where(final[5,orre] > 1. + 8./final[8,orre]) - final[5,orre,rI] = 1. - spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), final[0,orre,:],k=3) - dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) - NN = np.average(dlambda_dx) - dlambda_dx /= NN - final[9,orre] = final[5,orre] * (dlambda_dx ** 1) - final[10,orre] = final[6,orre] / (dlambda_dx ** 2) - - else: - rw,rf = final[0,orre],final[1,orre] - cbl = pucherosutils.fit_blaze(rw,rf) - ratio = np.polyval(cbl,rw) - final[3,orre] = rf/ratio - final[4,orre] = final[2,orre]*(ratio**2) - final[5,orre] = ratio / np.sqrt( ratio / GAIN + (RON/GAIN)**2 ) - medflx = np.zeros(len(final[3,orre,:])) - nI = np.where(np.isnan(final[3,orre])==False)[0] - nJ = np.where(np.isnan(final[3,orre])==True)[0] - medflx = scipy.signal.medfilt(final[3,orre,:][nI],3) - res = final[3,orre,:][nI] - medflx - dev = np.sqrt(np.var(res)) - I = np.where(final[3,orre,:] > 1. + 5*dev)[0] - final[3,orre][I]=1. - final[3,orre][nJ]=1. - - - order += 1 - orre += 1 - - if (not JustExtract): - spec = final.copy() - #DoClass = False - if DoClass: - print '\t\tSpectral Analysis:' - # spectral analysis - # First, query SIMBAD with the object name - query_success = False - query_success,sp_type_query = GLOBALutils.simbad_query_obname(nombre) - # Now, query SIMBAD by coordinates if above not successful - if (not query_success): - query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query - - hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) - - pars_file = dirout + obj.split('/')[-1][:-4]+'_stellar_pars.txt' - - if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" - T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec,model_path=models_path,npools=npools) - line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) - f = open(pars_file,'w') - f.write(line) - f.close() - - else: - print "\t\t\tAtmospheric parameters loaded from file:" - T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 - - else: - T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 - - T_eff_epoch = T_eff - logg_epoch = logg - Z_epoch = Z - vsini_epoch = vsini - vel0_epoch = vel0 - hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) - hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) - hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - - print "\t\tRadial Velocity analysis:" - # assign mask - sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - - # Read in mask - ml, mh, weight = np.loadtxt(mask,unpack=True) - ml_v = GLOBALutils.ToVacuum( ml ) - mh_v = GLOBALutils.ToVacuum( mh ) - - # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS - av_m = 0.5*( ml_v + mh_v ) - ml_v -= 5*(av_m - ml_v) - mh_v += 5*(mh_v - av_m) - mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m - - #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' - - disp = GLOBALutils.get_disp(obname, reffile=reffile) - - if disp == 0: - known_sigma = False - if vsini != -999 and vsini != 0.: - disp = vsini - else: - disp = 3. + + know_moon = False + if obj.split('/')[-1] in spec_moon: + I = np.where(obj.split('/')[-1] == spec_moon)[0] + know_moon = True + here_moon = use_moon[I] + + print("\t--> Working on image: ", obj) + index1,index2 = -1,-1 + h = pyfits.open(obj) + mjd,mjd0 = pucherosutils.mjd_fromheader(h) + hd = pyfits.getheader(obj) + nombre = pucherosutils.search_name(obj) + exptime = hd['EXPTIME'] + + print("\t\tObject name:",nombre) + + altitude = 1450. + latitude = -(33. + 16./60. + 9./3600.) + longitude = -(70. + 32./60. + 4./3600.) + known_coords = False + sp,ra,dec,known_coords = pucherosutils.get_coords(nombre,mjd) + epoch = 2000. + + ra2,dec2 = GLOBALutils.getcoords(obname,mjd,filen=reffile) + if ra2 !=0 and dec2 != 0: + ra = ra2 + dec = dec2 + known_coords = True + elif known_coords: + print('\t\tUsing the coordinates from symbad query.') + else: + print('\t\tUnknown coordinate for this object.') + + if known_coords: + iers = GLOBALutils.JPLiers( baryc_dir, mjd-999.0, mjd+999.0 ) + obsradius, R0 = GLOBALutils.JPLR0( latitude, altitude) + obpos = GLOBALutils.obspos( longitude, obsradius, R0 ) + jplephem.set_ephemeris_dir( baryc_dir , ephemeris ) + jplephem.set_observer_coordinates( obpos[0], obpos[1], obpos[2] ) + res = jplephem.doppler_fraction(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + lbary_ltopo = 1.0 + res['frac'][0] + bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 + print("\t\tBarycentric velocity:", bcvel_baryc) + res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) + mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) + + else: + bcvel_baryc = 0. + mbjd = mjd + + # Moon Phase Calculations + gobs = ephem.Observer() + gobs.name = 'ODUC' + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) + + gobs.date = hd['DATE-OBS'].replace('T',' ') + + mephem = ephem.Moon() + mephem.compute(gobs) + Mcoo = jplephem.object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Mp = jplephem.barycentric_object_track("Moon", int(mjd), float(mjd%1), 1, 0.0) + Sp = jplephem.barycentric_object_track("Sun", int(mjd), float(mjd%1), 1, 0.0) + res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) + lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) + refvel = bcvel_baryc + moonvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) + + + #print utils.search_name(obj) + nama = pucherosutils.search_name(obj)+'_'+hd['DATE-OBS'][:10]+'_'+hd['DATE-OBS'][11:13]+'-'+hd['DATE-OBS'][14:16]+'-'+hd['DATE-OBS'][17:] + obj_fits = dirout+nama+'.spec.fits.S' + bkg_obj_fits = dirout+'Bkg_'+nama+'.fits' + obj_fits_simple = dirout+nama+'.spec.simple.fits.S' + + + if ( os.access(obj_fits,os.F_OK) == False ) or \ + ( os.access(obj_fits_simple,os.F_OK) == False ) or \ + (force_sci_extract): + print("\t\tNo previous extraction or extraction forced for science file", obj, "extracting...") + + dat = pyfits.getdata(obj).astype('float') + dat -= MasterBias + if len(MasDarl)>0 and dark_corr: + dat -= pucherosutils.get_dark(MasDarl, hd['EXPTIME']) + dat = dat.T + + drift, c_new = GLOBALutils.get_drift(dat,P,c_all,pii=512,win=5) + print('\t\ty drift:', drift) + + P_new = GLOBALutils.shift_P(P,drift,c_new,ext_aperture) + + if back_corr: + Centers = np.zeros((len(c_new),dat.shape[1])) + for i in range(c_new.shape[0]): + Centers[i,:]=scipy.polyval(c_new[i,:],np.arange(len(Centers[i,:]))) + bac = GLOBALutils.get_scat(dat, Centers,span=5,allow_neg=True) + dat -= bac + + if not have_flat: + P_new = GLOBALutils.obtain_P(dat,c_new,ext_aperture,RON,\ + GAIN,NSigma_Marsh, S_Marsh, \ + N_Marsh, Marsh_alg, 0,1023, npools) + obj_Ss = GLOBALutils.simple_extraction(dat,c_new,ext_aperture,0,1023,npools) + obj_S = GLOBALutils.optimal_extraction(dat,P_new,c_new,ext_aperture,\ + RON,GAIN,S_Marsh,NCosmic_Marsh,0,1023,npools) + + obj_Ss = obj_Ss[::-1] + obj_S = obj_S[::-1] + + # save as fits file + if (os.access(obj_fits,os.F_OK)): + os.remove( obj_fits ) + if (os.access(obj_fits_simple,os.F_OK)): + os.remove( obj_fits_simple ) + + hdu = pyfits.PrimaryHDU( obj_S ) + hdu.writeto( obj_fits ) + hdu = pyfits.PrimaryHDU( obj_Ss ) + hdu.writeto( obj_fits_simple ) + + else: + obj_S = pyfits.getdata(obj_fits) + obj_Ss = pyfits.getdata(obj_fits_simple) + + deltat=1000 + i = 0 + while i < len(thtimes): + if abs(thtimes[i]-mjd) < deltat: + index1 = i + deltat = abs(thtimes[i]-mjd) + i+=1 + + + if mjd < thtimes[0]: + print("Problem with ThAr and science times") + index1 = 0 + index2 = 0 + indexx = 0 + elif mjd > thtimes[-1]: + print("Problem with ThAr and science times") + index1 = -1 + index2 = -1 + indexx = -1 + else: + for i in range(len(thtimes)-1): + if mjd >= thtimes[i] and mjd < thtimes[i+1]: + index1 = i + index2 = i+1 + if abs(mjd - thtimes[i]) < abs(mjd - thtimes[i+1]): + indexx = i + else: + indexx = i+1 + break + + #print ThAr_ref[index1], obj, ThAr_ref[index2] + hdth = pyfits.getheader(ThAr_ref[index1]) + wavsol_pkl = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + pdict = pickle.load(open(wavsol_pkl,'r')) + global1 = pdict['p1'] + All_Pixel_Centers = pdict['G_pix'] + All_Orders = pdict['G_ord'] + All_Wavelengths = pdict['G_wav'] + rms_ms = pdict['rms_ms'] + All_Residuals = pdict['G_res'] + All_Centroids = pdict['All_Centroids'] + All_Sigmas = pdict['All_Sigmas'] + trans = pdict['trans'] + or0 = pdict['or0'] + orwa = pdict['orwa'] + oro0 = pdict['oro0'] + + hdth = pyfits.getheader(ThAr_ref[index2]) + wavsol_pkl2 = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + pdict2 = pickle.load(open(wavsol_pkl2,'r')) + global2 = pdict2['p1'] + + hdth = pyfits.getheader(ThAr_ref[indexx]) + wavsol_pklx = dirout+'ThAr'+hdth['DATE-OBS'][:10]+'_'+hdth['DATE-OBS'][11:13]+'-'+hdth['DATE-OBS'][14:16]+'-'+hdth['DATE-OBS'][17:]+'.wavsolpars.pkl' + pdictx = pickle.load(open(wavsol_pklx,'r')) + globalx = pdictx['p1'] + + if JustExtract: + final = np.zeros( [6, nup,np.shape(obj_S)[2]] ) + else: + final = np.zeros( [11, nup,np.shape(obj_S)[2]] ) + hdu = pyfits.PrimaryHDU( final ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MJD', mjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH MBJD', mbjd) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START DATE', hdth['DATE-OBS'][:10] ) + hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', hdth['DATE-OBS'][:11]) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',hdth['EXPTIME']) + hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY) / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', nombre) + try: + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',ra) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',dec) + hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',ra) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',dec) + except: + None + hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) + hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS ALTITUDE',altitude) + + equis = np.arange( np.shape(obj_S)[2] ) + order = orwa + orre = 0 + + while orre < nup: + + m = order + oro0 + chebs = GLOBALutils.Calculate_chebs(equis, m, order0=oro0, npix=len(equis), ntotal=nup, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol1 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global1,chebs,ncoef_x,ncoef_m) + WavSol2 = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(global2,chebs,ncoef_x,ncoef_m) + WavSolx = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(globalx,chebs,ncoef_x,ncoef_m) + bestWavSol = (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(best_p1,chebs,ncoef_x,ncoef_m) + + if thtimes[index2] == thtimes[index1]: + WavSol = WavSol1 + else: + pen = (WavSol2 - WavSol1) / (thtimes[index2] - thtimes[index1]) + coe = WavSol2 - pen* thtimes[index2] + WavSol = pen*mjd + coe + if thtck == 0.: + p_shift = 0. + else: + p_shift = scipy.interpolate.splev(mjd, thtck, der=0) + p_shift = 0 + WavSol = bestWavSol * (1.0 + 1.0e-6*p_shift) + final[0,orre,:] = GLOBALutils.ToVacuum(WavSol[::-1]) + + #final[0,orre,:] = WavSolx[::-1] + if len(np.where(np.isnan(obj_S[orre,2,:][::-1]) == True) [0]) < 100: + final[1,orre,:] = obj_S[orre,1,:][::-1] # ...flux in ADU (optimal extraction)... + final[2,orre,:] = obj_S[orre,2,:][::-1] # ...and 1/variance... + if JustExtract==False: + final[3,orre,:] = final[1,orre,:] / sm_flat[orre,:][::-1] # ...flux (optimal)/flat... + final[4,orre,:] = final[2,orre,:] * (sm_flat[orre,:][::-1] ** 2) # ...and 1/variance * flat**2... + cont = GLOBALutils.get_cont_single(final[0,orre,:],final[3,orre,:],final[4,orre,:], nc = 3) + ratio = np.polyval(cont,final[0,orre]) + final[6,orre,:] = final[4,orre,:] * (ratio**2) + final[7,orre,:] = ratio + final[8,orre,:] = ratio * sm_flat[orre,:][::-1] / np.sqrt( ratio * sm_flat[orre,:][::-1] / GAIN + (RON/GAIN)**2 ) + final[5,orre,:] = final[3,orre,:] / ratio + nJ = np.where(np.isnan(final[5,orre])==True)[0] + nJ2 = np.where(np.isinf(final[5,orre])==True)[0] + final[5,orre,nJ] = 1.0 + final[5,orre,nJ2] = 1.0 + #plot(final[8,orre]) + rI = np.where(final[5,orre] > 1. + 8./final[8,orre]) + final[5,orre,rI] = 1. + spl = scipy.interpolate.splrep(np.arange(WavSol.shape[0]), final[0,orre,:],k=3) + dlambda_dx = scipy.interpolate.splev(np.arange(WavSol.shape[0]), spl, der=1) + NN = np.average(dlambda_dx) + dlambda_dx /= NN + final[9,orre] = final[5,orre] * (dlambda_dx ** 1) + final[10,orre] = final[6,orre] / (dlambda_dx ** 2) + + else: + rw,rf = final[0,orre],final[1,orre] + cbl = pucherosutils.fit_blaze(rw,rf) + ratio = np.polyval(cbl,rw) + final[3,orre] = rf/ratio + final[4,orre] = final[2,orre]*(ratio**2) + final[5,orre] = ratio / np.sqrt( ratio / GAIN + (RON/GAIN)**2 ) + medflx = np.zeros(len(final[3,orre,:])) + nI = np.where(np.isnan(final[3,orre])==False)[0] + nJ = np.where(np.isnan(final[3,orre])==True)[0] + medflx = scipy.signal.medfilt(final[3,orre,:][nI],3) + res = final[3,orre,:][nI] - medflx + dev = np.sqrt(np.var(res)) + I = np.where(final[3,orre,:] > 1. + 5*dev)[0] + final[3,orre][I]=1. + final[3,orre][nJ]=1. + + + order += 1 + orre += 1 + + if (not JustExtract): + spec = final.copy() + #DoClass = False + if DoClass: + print('\t\tSpectral Analysis:') + # spectral analysis + # First, query SIMBAD with the object name + query_success = False + query_success,sp_type_query = GLOBALutils.simbad_query_obname(nombre) + # Now, query SIMBAD by coordinates if above not successful + if (not query_success): + query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) + + hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) + + pars_file = dirout + obj.split('/')[-1][:-4]+'_stellar_pars.txt' + + if os.access(pars_file,os.F_OK) == False or force_stellar_pars: + print("\t\t\tEstimating atmospheric parameters:") + T_eff, logg, Z, vsini, vel0, ccf = correlation.CCF(spec,model_path=models_path,npools=npools) + line = "%6d %4.1f %4.1f %8.1f %8.1f\n" % (T_eff,logg, Z, vsini, vel0) + f = open(pars_file,'w') + f.write(line) + f.close() + + else: + print("\t\t\tAtmospheric parameters loaded from file:") + T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) + + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) + + else: + T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 + + T_eff_epoch = T_eff + logg_epoch = logg + Z_epoch = Z + vsini_epoch = vsini + vel0_epoch = vel0 + hdu = GLOBALutils.update_header(hdu,'HIERARCH TEFF', float(T_eff)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH LOGG', float(logg)) + hdu = GLOBALutils.update_header(hdu,'HIERARCH Z', Z) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) + hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) + + print("\t\tRadial Velocity analysis:") + # assign mask + sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') + print("\t\t\tWill use",sp_type,"mask for CCF.") + + # Read in mask + ml, mh, weight = np.loadtxt(mask,unpack=True) + ml_v = GLOBALutils.ToVacuum( ml ) + mh_v = GLOBALutils.ToVacuum( mh ) + + # make mask larger accounting for factor ~2 lower res in CORALIE w/r to HARPS + av_m = 0.5*( ml_v + mh_v ) + ml_v -= 5*(av_m - ml_v) + mh_v += 5*(mh_v - av_m) + mask_hw_kms = (GLOBALutils.Constants.c/1e3) * 0.5*(mh_v - ml_v) / av_m + + #sigma_fout = stellar_pars_dir + obname + '_' +'sigma.txt' + + disp = GLOBALutils.get_disp(obname, reffile=reffile) + + if disp == 0: + known_sigma = False + if vsini != -999 and vsini != 0.: + disp = vsini + else: + disp = 3. + else: + known_sigma = True + + if disp < 10: + disp = 5 + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide + + print('\t\t\tComputing the CCF...') + cond = True + while (cond): + # first rough correlation to find the minimum + vels, xc_full, sn, nlines_ccf, W_ccf = \ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, 1., vel_width=300,vel_step=1.,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf) + #print W_ccf + # Normalize the continuum of the CCF robustly with lowess + yy = scipy.signal.medfilt(xc_av,11) + pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) + tck1 = scipy.interpolate.splrep(vels,pred,k=1) + xc_av_orig = xc_av.copy() + xc_av /= pred + vel0_xc = vels[ np.argmin( xc_av ) ] + rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ + xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc + + xc_av_rough = xc_av + vels_rough = vels + if disp > 30: + disp = 30. + vel_width = np.maximum( 40.0, 6*disp ) + + vels, xc_full, sn, nlines_ccf, W_ccf =\ + GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, 1., vel_width=vel_width,vel_step=1.,\ + spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) + #print W_ccf + + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf) + pred = scipy.interpolate.splev(vels,tck1) + xc_av /= pred + + if sp_type == 'M5': + moon_sig = 5.0 + elif sp_type == 'K5': + moon_sig = 6.6 else: + moon_sig = 9.0 + + p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ + sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) + + #ldc = CoralieUtils.get_ldc(T_eff, logg, Z, 1.0, ldfile = 'lin_coe_sloan2.dat') + #p1R, ROTmodel = CoralieUtils.XC_Final_Fit_Rot( vels, xc_av, ldc = ldc, vsini = vsini ) + moonmatters = False + if (know_moon and here_moon): + moonmatters = True + ismoon = True + confused = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) + moon_flag = 1 + else: + confused = False + ismoon = False + p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 + moon_flag = 0 + + bspan = GLOBALutils.calc_bss(vels,xc_av) + SP = bspan[0] + + #print 'Bisector span:', SP + if (not known_sigma): + disp = np.floor(p1gau[2]) + if (disp < 3.0): + disp = 3.0 + mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) + ml_v = av_m - mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True + else: + cond = False - if disp < 10: - disp = 5 - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' - cond = True - while (cond): - # first rough correlation to find the minimum - vels, xc_full, sn, nlines_ccf, W_ccf = \ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, 0, 1., vel_width=300,vel_step=1.,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf) - #print W_ccf - # Normalize the continuum of the CCF robustly with lowess - yy = scipy.signal.medfilt(xc_av,11) - pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) - tck1 = scipy.interpolate.splrep(vels,pred,k=1) - xc_av_orig = xc_av.copy() - xc_av /= pred - vel0_xc = vels[ np.argmin( xc_av ) ] - rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ - xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc - - xc_av_rough = xc_av - vels_rough = vels - if disp > 30: - disp = 30. - vel_width = np.maximum( 40.0, 6*disp ) - - vels, xc_full, sn, nlines_ccf, W_ccf =\ - GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, 1., vel_width=vel_width,vel_step=1.,\ - spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300) - #print W_ccf - - xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=0.0, Simple=True, W=W_ccf) - pred = scipy.interpolate.splev(vels,tck1) - xc_av /= pred - - if sp_type == 'M5': - moon_sig = 5.0 - elif sp_type == 'K5': - moon_sig = 6.6 - else: - moon_sig = 9.0 - - p1,XCmodel,p1gau,XCmodelgau,Ls2 = GLOBALutils.XC_Final_Fit( vels, xc_av ,\ - sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = False) - - #ldc = CoralieUtils.get_ldc(T_eff, logg, Z, 1.0, ldfile = 'lin_coe_sloan2.dat') - #p1R, ROTmodel = CoralieUtils.XC_Final_Fit_Rot( vels, xc_av, ldc = ldc, vsini = vsini ) - moonmatters = False - if (know_moon and here_moon): - moonmatters = True - ismoon = True - confused = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = GLOBALutils.XC_Final_Fit( vels, xc_av , sigma_res = 4, horder=8, moonv = refvel, moons = moon_sig, moon = True) - moon_flag = 1 - else: - confused = False - ismoon = False - p1_m,XCmodel_m,p1gau_m,XCmodelgau_m,Ls2_m = p1,XCmodel,p1gau,XCmodelgau,Ls2 - moon_flag = 0 - - bspan = GLOBALutils.calc_bss(vels,xc_av) - SP = bspan[0] - - #print 'Bisector span:', SP - if (not known_sigma): - disp = np.floor(p1gau[2]) - if (disp < 3.0): - disp = 3.0 - mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) - ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide - known_sigma = True - else: - cond = False + xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} - xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ + 'lunation':lunation,'mephem':mephem,'texp':exptime} - moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':exptime} + pkl_xc = dirout + obj.split('/')[-1][:-4]+nombre+'_XC_'+sp_type+'.pkl' + pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - pkl_xc = dirout + obj.split('/')[-1][:-4]+nombre+'_XC_'+sp_type+'.pkl' - pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) + ccf_pdf = dirout + 'proc/' + obj.split('/')[-1][:-4] + nombre + '_XCs_' + sp_type + '.pdf' - ccf_pdf = dirout + 'proc/' + obj.split('/')[-1][:-4] + nombre + '_XCs_' + sp_type + '.pdf' + if not avoid_plot: + GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) - if not avoid_plot: - GLOBALutils.plot_CCF(xc_dict,moon_dict,path=ccf_pdf) + SNR_5130 = np.median(spec[8,30,400:601] ) + airmass = -999 + seeing = -999 - SNR_5130 = np.median(spec[8,30,400:601] ) - airmass = -999 - seeing = -999 - + if sp_type == 'G2': + if T_eff < 6000: + A = 0.06544 + B = 0.00146 + D = 0.24416 + C = 0.00181 + else: + A = 0.09821 + B = 0.00014 + D = 0.33491 + C = 0.00113 + elif sp_type == 'K5': + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + else: + A = 0.05348 + B = 0.00147 + D = 0.20695 + C = 0.00321 + + RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) + depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) + + if depth_fact >= 1.: + RVerr2 = -999.000 + else: if sp_type == 'G2': - if T_eff < 6000: - A = 0.06544 - B = 0.00146 - D = 0.24416 - C = 0.00181 - else: - A = 0.09821 - B = 0.00014 - D = 0.33491 - C = 0.00113 - elif sp_type == 'K5': - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 + depth_fact = (1 - 0.62) / (1 - depth_fact) else: - A = 0.05348 - B = 0.00147 - D = 0.20695 - C = 0.00321 - - RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) - depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) - - if depth_fact >= 1.: - RVerr2 = -999.000 - else: - if sp_type == 'G2': - depth_fact = (1 - 0.62) / (1 - depth_fact) - else: - depth_fact = (1 - 0.59) / (1 - depth_fact) - RVerr2 = RVerr * depth_fact - if (RVerr2 <= 0.009): - RVerr2 = 0.009 - - if RVerr2 < 0.1: - RVerr2 = 0.1 - BSerr = D / float(np.round(SNR_5130)) + C - - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) - RVerr2 = np.around(RVerr2,4) - BSerr = np.around(BSerr,4) - - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) - - bjd_out = 2400000.5 + mbjd - T_eff_err = 100 - logg_err = 0.5 - Z_err = 0.5 - vsini_err = 2 - XC_min = np.abs(np.around(np.min(XCmodel),2)) - - SNR_5130 = np.around(SNR_5130) - SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) - - disp_epoch = np.around(p1gau_m[2],1) - hdu = GLOBALutils.update_header(hdu,'RV', RV) - hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) - hdu = GLOBALutils.update_header(hdu,'BS', BS) - hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) - hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) - hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) - hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) - hdu = GLOBALutils.update_header(hdu,'INST', 'PUCHEROS') - hdu = GLOBALutils.update_header(hdu,'RESOL', '20000') - hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') - hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) - hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) - - line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f pucheros ceres 20000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ - (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - exptime, SNR_5130_R, ccf_pdf) - f_res.write(line_out) - - fout = 'proc/'+nama+'_final.fits' - if (os.access( dirout + fout,os.F_OK)): - os.remove( dirout + fout) - hdu.writeto( dirout + fout ) - -f_res.close() \ No newline at end of file + depth_fact = (1 - 0.59) / (1 - depth_fact) + RVerr2 = RVerr * depth_fact + if (RVerr2 <= 0.009): + RVerr2 = 0.009 + + if RVerr2 < 0.1: + RVerr2 = 0.1 + BSerr = D / float(np.round(SNR_5130)) + C + + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) + RVerr2 = np.around(RVerr2,4) + BSerr = np.around(BSerr,4) + + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) + + bjd_out = 2400000.5 + mbjd + T_eff_err = 100 + logg_err = 0.5 + Z_err = 0.5 + vsini_err = 2 + XC_min = np.abs(np.around(np.min(XCmodel),2)) + + SNR_5130 = np.around(SNR_5130) + SNR_5130_R = np.around(SNR_5130*np.sqrt(2.9)) + + disp_epoch = np.around(p1gau_m[2],1) + hdu = GLOBALutils.update_header(hdu,'RV', RV) + hdu = GLOBALutils.update_header(hdu,'RV_E', RVerr2) + hdu = GLOBALutils.update_header(hdu,'BS', BS) + hdu = GLOBALutils.update_header(hdu,'BS_E', BSerr) + hdu = GLOBALutils.update_header(hdu,'DISP', disp_epoch) + hdu = GLOBALutils.update_header(hdu,'SNR', SNR_5130) + hdu = GLOBALutils.update_header(hdu,'SNR_R', SNR_5130_R) + hdu = GLOBALutils.update_header(hdu,'INST', 'PUCHEROS') + hdu = GLOBALutils.update_header(hdu,'RESOL', '20000') + hdu = GLOBALutils.update_header(hdu,'PIPELINE', 'CERES') + hdu = GLOBALutils.update_header(hdu,'XC_MIN', XC_min) + hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) + + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f pucheros ceres 20000 %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ + (obname, bjd_out, RV, RVerr2, BS, BSerr, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ + exptime, SNR_5130_R, ccf_pdf) + f_res.write(line_out) + + fout = 'proc/'+nama+'_final.fits' + if (os.access( dirout + fout,os.F_OK)): + os.remove( dirout + fout) + hdu.writeto( dirout + fout ) + +f_res.close() diff --git a/pucheros/pucherosutils.py b/pucheros/pucherosutils.py index 5ab19c8..1914f72 100644 --- a/pucheros/pucherosutils.py +++ b/pucheros/pucherosutils.py @@ -1,6 +1,7 @@ +from __future__ import print_function import sys import matplotlib -matplotlib.use("Agg") +matplotlib.use("Agg") base = '../' sys.path.append(base+"utils/GLOBALutils") @@ -12,265 +13,269 @@ import os import glob import tempfile -import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + import pycurl from pylab import * def is_there(string, word): - l=len(word) - i=0 - ist = False - while i < len(string)-l: - if string[i:i+l] == word: - ist = True - i+=1 - return ist + l=len(word) + i=0 + ist = False + while i < len(string)-l: + if string[i:i+l] == word: + ist = True + i+=1 + return ist def search_name(obj): - name = obj.split('/')[-1] - try: - name = name.split('_')[1] - except: - name = name.split('_')[1] - #print 'NAME:', name - return name + name = obj.split('/')[-1] + try: + name = name.split('_')[1] + except: + name = name.split('_')[1] + #print 'NAME:', name + return name def FileClassify(path,log): - biases = [] - flats = [] - img_flats = [] - fib_flats = [] - objects = [] - darks = [] - thars = [] - - - lines = [] - dates = [] - archs = glob.glob(path+'*.fit') - - bad_files = [] - if os.access(path+'bad_files.txt',os.F_OK): - bf = open(path+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(path+line[:-1]) - - bf.close() - for arch in archs: - dump = False - for bf in bad_files: - if arch == bf: - dump = True - break - if not dump: - - h = pyfits.open(arch) - - #print h[0].header['XBINNING'], h[0].header['YBINNING'], arch - if h[0].header['XBINNING'] == 1 and h[0].header['YBINNING'] == 1: - if h[0].header['IMAGETYP'] == 'Light Frame' or h[0].header['IMAGETYP'] == 'LIGHT': - if 'flat' in arch: - flats.append(arch) - else: - name = h[0].header['OBJECT'] - expt = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'] - line = "%-15s %8.2f %8s %s\n" % (name, expt, date, arch) - - ye = float(date[:4]) - mo = float(date[5:7]) - da = float(date[8:10]) - ho = float(date[11:13])-4.0 - mi = float(date[14:15]) - se = float(date[17:]) - - lines.append(line) - dates.append( jd( ye,mo,da,ho,mi,se ) ) - #f.write(line) - - if is_there(arch.lower(),'thar') or is_there(arch.lower(),'th_ar'): - thars.append(arch) - else: - objects.append(arch) - - elif h[0].header['IMAGETYP'] == 'Bias Frame' or h[0].header['IMAGETYP'] == 'BIAS': - biases.append(arch) - elif (h[0].header['IMAGETYP'] == 'Flat Frame' or h[0].header['IMAGETYP'] == 'FLAT') and arch != 'MasterFlat.fits': - # Now check which kind of flat it is. - # Maybe a "surface" flat... - if(is_there(arch.lower(),'imgflat')): - img_flats.append(arch) - # ...a fibre flat... - elif(is_there(arch.lower(),'fibre')): - fib_flats.append(arch) - # (use them for traces, blaze and col-to-col) - flats.append(arch) - # ...else, it is a screen flat (w/difussor): - else: - flats.append(arch) - elif h[0].header['IMAGETYP'] == 'Dark Frame' or h[0].header['IMAGETYP'] == 'DARK': - if h[0].header['EXPTIME']!=0.0: - darks.append(arch) - - h.close() - lines = np.array(lines) - dates = np.array(dates) - I = np.argsort(dates) - lines = lines[I] - f = open(log,'w') - for line in lines: - f.write(line) - f.close() - return biases,flats,img_flats,fib_flats,objects,thars,darks + biases = [] + flats = [] + img_flats = [] + fib_flats = [] + objects = [] + darks = [] + thars = [] + + + lines = [] + dates = [] + archs = glob.glob(path+'*.fit') + + bad_files = [] + if os.access(path+'bad_files.txt',os.F_OK): + bf = open(path+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(path+line[:-1]) + + bf.close() + for arch in archs: + dump = False + for bf in bad_files: + if arch == bf: + dump = True + break + if not dump: + + h = pyfits.open(arch) + + #print h[0].header['XBINNING'], h[0].header['YBINNING'], arch + if h[0].header['XBINNING'] == 1 and h[0].header['YBINNING'] == 1: + if h[0].header['IMAGETYP'] == 'Light Frame' or h[0].header['IMAGETYP'] == 'LIGHT': + if 'flat' in arch: + flats.append(arch) + else: + name = h[0].header['OBJECT'] + expt = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'] + line = "%-15s %8.2f %8s %s\n" % (name, expt, date, arch) + + ye = float(date[:4]) + mo = float(date[5:7]) + da = float(date[8:10]) + ho = float(date[11:13])-4.0 + mi = float(date[14:15]) + se = float(date[17:]) + + lines.append(line) + dates.append( jd( ye,mo,da,ho,mi,se ) ) + #f.write(line) + + if is_there(arch.lower(),'thar') or is_there(arch.lower(),'th_ar'): + thars.append(arch) + else: + objects.append(arch) + + elif h[0].header['IMAGETYP'] == 'Bias Frame' or h[0].header['IMAGETYP'] == 'BIAS': + biases.append(arch) + elif (h[0].header['IMAGETYP'] == 'Flat Frame' or h[0].header['IMAGETYP'] == 'FLAT') and arch != 'MasterFlat.fits': + # Now check which kind of flat it is. + # Maybe a "surface" flat... + if(is_there(arch.lower(),'imgflat')): + img_flats.append(arch) + # ...a fibre flat... + elif(is_there(arch.lower(),'fibre')): + fib_flats.append(arch) + # (use them for traces, blaze and col-to-col) + flats.append(arch) + # ...else, it is a screen flat (w/difussor): + else: + flats.append(arch) + elif h[0].header['IMAGETYP'] == 'Dark Frame' or h[0].header['IMAGETYP'] == 'DARK': + if h[0].header['EXPTIME']!=0.0: + darks.append(arch) + + h.close() + lines = np.array(lines) + dates = np.array(dates) + I = np.argsort(dates) + lines = lines[I] + f = open(log,'w') + for line in lines: + f.write(line) + f.close() + return biases,flats,img_flats,fib_flats,objects,thars,darks def get_rg(): - return 9.6,1.6 + return 9.6,1.6 def MedianCombine(ImgList,zero_bo,zero,dark_bo=False, dlist = []): - """ - Median combine a list of images - """ - - hf = pyfits.getheader(ImgList[0]) - - if zero_bo: - Master = pyfits.getdata(zero) - if dark_bo: - Dark = get_dark(dlist,hf['EXPTIME']) - - n = len(ImgList) - - if n==0: - raise ValueError("empty list provided!") - - d = pyfits.getdata(ImgList[0]) - - if zero_bo: - d = d - Master - if dark_bo: - d = d - Dark - - factor = 1.25 - - if (n < 3): - factor = 1 - - #ronoise = factor * h.header['ENOISE'] / np.sqrt(n) - #gain = h.header['EGAIN'] - ronoise,gain=get_rg() - if (n == 1): - return d, ronoise, gain - - else: - for i in range(n-1): - h = pyfits.getdata(ImgList[i+1]) - if zero_bo: - h = h-Master - if dark_bo: - h = h-Dark - d = np.dstack((d,h)) - - return np.median(d,axis=2), ronoise/np.sqrt(n), gain + """ + Median combine a list of images + """ + + hf = pyfits.getheader(ImgList[0]) + + if zero_bo: + Master = pyfits.getdata(zero) + if dark_bo: + Dark = get_dark(dlist,hf['EXPTIME']) + + n = len(ImgList) + + if n==0: + raise ValueError("empty list provided!") + + d = pyfits.getdata(ImgList[0]) + + if zero_bo: + d = d - Master + if dark_bo: + d = d - Dark + + factor = 1.25 + + if (n < 3): + factor = 1 + + #ronoise = factor * h.header['ENOISE'] / np.sqrt(n) + #gain = h.header['EGAIN'] + ronoise,gain=get_rg() + if (n == 1): + return d, ronoise, gain + + else: + for i in range(n-1): + h = pyfits.getdata(ImgList[i+1]) + if zero_bo: + h = h-Master + if dark_bo: + h = h-Dark + d = np.dstack((d,h)) + + return np.median(d,axis=2), ronoise/np.sqrt(n), gain def get_dark(darks,t): - exact = 0 - dts = [] - for dark in darks: - hd = pyfits.getheader(dark) - dt = hd['EXPTIME'] - dts.append(dt) - if dt == t: - #print 'dark:',dark - DARK = pyfits.getdata(dark) - exact = 1 - - dts = np.array(dts) - if exact == 0: - if t < dts.min(): - I = np.where( dts == dts.min() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - elif t > dts.max(): - I = np.where( dts == dts.max() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - #print darks[I[0]] - else: - tmin = dts.min() - tmax = dts.max() - I = np.where( dts == dts.min() )[0] - Dmin = pyfits.getdata(darks[I[0]]) - Dminname=darks[I[0]] - I = np.where( dts == dts.max() )[0] - Dmax = pyfits.getdata(darks[I[0]]) - Dmaxname = darks[I[0]] - - i = 0 - while i < len(dts): - if dts[i] < t and dts[i] > tmin: - tmin = dts[i] - Dminname = darks[i] - Dmin = pyfits.getdata(darks[i]) - elif dts[i] > t and dts[i] < tmax: - tmax = dts[i] - Dmaxname = darks[i] - Dmax = pyfits.getdata(darks[i]) - i+=1 - - num = Dmax - Dmin - den = tmax-tmin - m = num/den - n = Dmax - m*tmax - DARK = m*t+n - - return DARK + exact = 0 + dts = [] + for dark in darks: + hd = pyfits.getheader(dark) + dt = hd['EXPTIME'] + dts.append(dt) + if dt == t: + #print 'dark:',dark + DARK = pyfits.getdata(dark) + exact = 1 + + dts = np.array(dts) + if exact == 0: + if t < dts.min(): + I = np.where( dts == dts.min() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + elif t > dts.max(): + I = np.where( dts == dts.max() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + #print darks[I[0]] + else: + tmin = dts.min() + tmax = dts.max() + I = np.where( dts == dts.min() )[0] + Dmin = pyfits.getdata(darks[I[0]]) + Dminname=darks[I[0]] + I = np.where( dts == dts.max() )[0] + Dmax = pyfits.getdata(darks[I[0]]) + Dmaxname = darks[I[0]] + + i = 0 + while i < len(dts): + if dts[i] < t and dts[i] > tmin: + tmin = dts[i] + Dminname = darks[i] + Dmin = pyfits.getdata(darks[i]) + elif dts[i] > t and dts[i] < tmax: + tmax = dts[i] + Dmaxname = darks[i] + Dmax = pyfits.getdata(darks[i]) + i+=1 + + num = Dmax - Dmin + den = tmax-tmin + m = num/den + n = Dmax - m*tmax + DARK = m*t+n + + return DARK def jd(y,m,d,h,mins,s): - "Julian day is calculated here if it's needed" - MY = (m-14)/12 - y = MY+y - return ( 1461 * ( y + 4800 ) ) / 4 + ( 367 * ( m - 2 - 12*MY ) ) / 12 - ( 3 * ( ( y + 4900 ) / 100 ) ) / 4 + d -32077.5 + htosec(h,mins,s)/86400.0 + "Julian day is calculated here if it's needed" + MY = (m-14)/12 + y = MY+y + return ( 1461 * ( y + 4800 ) ) / 4 + ( 367 * ( m - 2 - 12*MY ) ) / 12 - ( 3 * ( ( y + 4900 ) / 100 ) ) / 4 + d -32077.5 + htosec(h,mins,s)/86400.0 def htosec(h,m,s): - "transform from hour,minute and seconds, to seconds" - return s+60.0*(m+60.0*h) + "transform from hour,minute and seconds, to seconds" + return s+60.0*(m+60.0*h) def fit_blaze(w,f,n=5): - warnings.simplefilter('ignore', np.RankWarning) - li = len(w) - co = np.polyfit(w,f,n) - res = f - np.polyval(co,w) - dev = np.sqrt(np.var(res)) - J1 = np.where(res < -1.5*dev)[0] - J2 = np.where(res > 3*dev)[0] - J = np.hstack((J1,J2)) - J = np.sort(J) - I = np.where( (res >= -1.5*dev) & (res <= 3*dev) )[0] - cond = True - if len(J)==0 or len(I) < .3*li: - cond = False - while cond: - w,f = w[I],f[I] - co = np.polyfit(w,f,n) - res = f - np.polyval(co,w) - dev = np.sqrt(np.var(res)) - J1 = np.where(res < -1.5*dev)[0] - J2 = np.where(res > 3*dev)[0] - J = np.hstack((J1,J2)) - J = np.sort(J) - I = np.where( (res >= -1.5*dev) & (res <= 3*dev) )[0] - cond = True - if len(J)==0 or len(I) < .3*li: - cond = False - return co + warnings.simplefilter('ignore', np.RankWarning) + li = len(w) + co = np.polyfit(w,f,n) + res = f - np.polyval(co,w) + dev = np.sqrt(np.var(res)) + J1 = np.where(res < -1.5*dev)[0] + J2 = np.where(res > 3*dev)[0] + J = np.hstack((J1,J2)) + J = np.sort(J) + I = np.where( (res >= -1.5*dev) & (res <= 3*dev) )[0] + cond = True + if len(J)==0 or len(I) < .3*li: + cond = False + while cond: + w,f = w[I],f[I] + co = np.polyfit(w,f,n) + res = f - np.polyval(co,w) + dev = np.sqrt(np.var(res)) + J1 = np.where(res < -1.5*dev)[0] + J2 = np.where(res > 3*dev)[0] + J = np.hstack((J1,J2)) + J = np.sort(J) + I = np.where( (res >= -1.5*dev) & (res <= 3*dev) )[0] + cond = True + if len(J)==0 or len(I) < .3*li: + cond = False + return co def mjd_fromheader(h): """ return modified Julian date from header """ - - datetu = h[0].header['DATE-OBS'] + + datetu = h[0].header['DATE-OBS'] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10])) ut = float(datetu[11:13]) + float(datetu[14:16])/60. + float(datetu[17:])/3600. mjd_start = mjd + ut/24.0 @@ -284,119 +289,119 @@ def mjd_fromheader(h): return mjd, mjd0 def get_coords(obname,mjd): - if obname.lower() == 'alphacent': - obname = 'alpha cent' - elif obname.lower() == 'alphaboo': - obname = 'alpha boo' - elif obname.lower() == 'hadar' or obname.lower() == 'betacen': - obname = 'beta cen' - elif obname.lower() == 'diphda': - obname = 'bet cet' - elif obname.lower() == 'betacar': - obname = 'beta car' - elif obname.lower() == 'betscl': - obname = 'bet scl' - elif obname.lower() == 'bvel': - obname = 'b vel' - elif obname.lower() == 'deltasco': - obname = 'del sco' - elif obname.lower() == 'delcen': - obname = 'del cen' - elif obname.lower() == 'epsilonaqr': - obname = 'eps aqr' - elif obname.lower() == 'epspsa': - obname = 'eps psa' - elif obname.lower() == 'etahya' or obname.lower() == 'ethahydra': - obname = 'eta Hya' - elif obname.lower() == 'etapsa': - obname = 'eta psa' - elif obname.lower() == 'etacen': - obname = 'eta cen' - elif obname.lower() == 'opup': - obname = 'o Pup' - elif obname.lower() == 'etacar': - obname = 'eta Car' - elif obname.lower() == 'agcar': - obname = 'ag Car' - elif obname.lower() == 'hrcar': - obname = 'hr Car' - elif obname.lower() == 'sslep': - obname = 'ss lep' - elif obname.lower() == 'thetavir': - obname = 'theta vir' - elif obname.lower() == 'mucen': - obname = 'mu cen' - elif obname.lower() == 'lesath': - obname = 'ups sco' - elif obname.lower() == 'mulup': - obname = 'mu lup' - elif obname.lower() == 'chioph': - obname = 'chi oph' - elif obname.lower() == 'dlup': - obname = 'd lup' - elif obname.lower() == '48lib': - obname = '48 lib' - elif obname.lower() == 'iotara': - obname = 'iot ara' - elif obname.lower() == 'qvtel': - obname = 'qv tel' - elif obname.lower() == 'taucet': - obname = 'tau cet' - elif obname.lower() == 'pi2ori': - obname = 'pi2 ori' - elif obname.lower() == 'zetapeg': - obname = 'zet peg' - elif obname.lower() == 'tpyx': - obname = 't pyx' - elif obname.lower() == 'omicronpup': - obname = 'omi pup' - - sp,ra,dec = 0,0,0 - (th,tfile) = tempfile.mkstemp(prefix='CP', text=True) - tf = open(tfile,'w') - tf.write("output console=off\n") - tf.write("output script=off\n") - tf.write("output error=merge\n") - tf.write("set limit 1\n") - tf.write("format object fmt1 \"%IDLIST(1) | %OTYPELIST(S) | %SP(S) | %COO(A) | %COO(D) | %PM(A) | %PM(D)\"\n") - tf.write("result full\n") - tf.write("query id %s\n" % ( obname ) ) - tf.close() - values = [("scriptFIle", (pycurl.FORM_FILE, tfile))] - output = StringIO.StringIO() - c = pycurl.Curl() - c.setopt(pycurl.URL, "http://simbad.harvard.edu/simbad/sim-script") - c.setopt(c.HTTPPOST, values) - c.setopt(pycurl.WRITEFUNCTION, output.write) - cond = True - while cond: - try: - c.perform() - except: - print 'Trying again to perform query to SIMBAD' - else: - cond = False - c.close() - result = output.getvalue() - lines = result.split('\n') - info = lines[6].split('|') - - if 'Unrecogniezd' in info[0] or 'not' in info[0]: - know = False - else: - know = True - sp,ra,dec,pmra,pmdec = info[2],info[3],info[4],info[5],info[6] - - if '~' in pmra: - pmra = '0.' - if '~' in pmdec: - pmdec = '0.' - - rad = ra.split() - decd = dec.split() - ra = float(rad[0])*360./24. + float(rad[1])*6./24. + float(rad[2])/240. + (float(pmra)/(3600*1000.))*((mjd-51544.5)/365.) - if float(decd[0])<0: - dec = -(np.absolute(float(decd[0])) + float(decd[1])/60. + float(decd[2])/3600.) + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) - else: - dec = float(decd[0]) + float(decd[1])/60. + float(decd[2])/3600. + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) - return sp,ra,dec,know \ No newline at end of file + if obname.lower() == 'alphacent': + obname = 'alpha cent' + elif obname.lower() == 'alphaboo': + obname = 'alpha boo' + elif obname.lower() == 'hadar' or obname.lower() == 'betacen': + obname = 'beta cen' + elif obname.lower() == 'diphda': + obname = 'bet cet' + elif obname.lower() == 'betacar': + obname = 'beta car' + elif obname.lower() == 'betscl': + obname = 'bet scl' + elif obname.lower() == 'bvel': + obname = 'b vel' + elif obname.lower() == 'deltasco': + obname = 'del sco' + elif obname.lower() == 'delcen': + obname = 'del cen' + elif obname.lower() == 'epsilonaqr': + obname = 'eps aqr' + elif obname.lower() == 'epspsa': + obname = 'eps psa' + elif obname.lower() == 'etahya' or obname.lower() == 'ethahydra': + obname = 'eta Hya' + elif obname.lower() == 'etapsa': + obname = 'eta psa' + elif obname.lower() == 'etacen': + obname = 'eta cen' + elif obname.lower() == 'opup': + obname = 'o Pup' + elif obname.lower() == 'etacar': + obname = 'eta Car' + elif obname.lower() == 'agcar': + obname = 'ag Car' + elif obname.lower() == 'hrcar': + obname = 'hr Car' + elif obname.lower() == 'sslep': + obname = 'ss lep' + elif obname.lower() == 'thetavir': + obname = 'theta vir' + elif obname.lower() == 'mucen': + obname = 'mu cen' + elif obname.lower() == 'lesath': + obname = 'ups sco' + elif obname.lower() == 'mulup': + obname = 'mu lup' + elif obname.lower() == 'chioph': + obname = 'chi oph' + elif obname.lower() == 'dlup': + obname = 'd lup' + elif obname.lower() == '48lib': + obname = '48 lib' + elif obname.lower() == 'iotara': + obname = 'iot ara' + elif obname.lower() == 'qvtel': + obname = 'qv tel' + elif obname.lower() == 'taucet': + obname = 'tau cet' + elif obname.lower() == 'pi2ori': + obname = 'pi2 ori' + elif obname.lower() == 'zetapeg': + obname = 'zet peg' + elif obname.lower() == 'tpyx': + obname = 't pyx' + elif obname.lower() == 'omicronpup': + obname = 'omi pup' + + sp,ra,dec = 0,0,0 + (th,tfile) = tempfile.mkstemp(prefix='CP', text=True) + tf = open(tfile,'w') + tf.write("output console=off\n") + tf.write("output script=off\n") + tf.write("output error=merge\n") + tf.write("set limit 1\n") + tf.write("format object fmt1 \"%IDLIST(1) | %OTYPELIST(S) | %SP(S) | %COO(A) | %COO(D) | %PM(A) | %PM(D)\"\n") + tf.write("result full\n") + tf.write("query id %s\n" % ( obname ) ) + tf.close() + values = [("scriptFIle", (pycurl.FORM_FILE, tfile))] + output = StringIO() + c = pycurl.Curl() + c.setopt(pycurl.URL, "http://simbad.harvard.edu/simbad/sim-script") + c.setopt(c.HTTPPOST, values) + c.setopt(pycurl.WRITEFUNCTION, output.write) + cond = True + while cond: + try: + c.perform() + except: + print('Trying again to perform query to SIMBAD') + else: + cond = False + c.close() + result = output.getvalue() + lines = result.split('\n') + info = lines[6].split('|') + + if 'Unrecogniezd' in info[0] or 'not' in info[0]: + know = False + else: + know = True + sp,ra,dec,pmra,pmdec = info[2],info[3],info[4],info[5],info[6] + + if '~' in pmra: + pmra = '0.' + if '~' in pmdec: + pmdec = '0.' + + rad = ra.split() + decd = dec.split() + ra = float(rad[0])*360./24. + float(rad[1])*6./24. + float(rad[2])/240. + (float(pmra)/(3600*1000.))*((mjd-51544.5)/365.) + if float(decd[0])<0: + dec = -(np.absolute(float(decd[0])) + float(decd[1])/60. + float(decd[2])/3600.) + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) + else: + dec = float(decd[0]) + float(decd[1])/60. + float(decd[2])/3600. + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) + return sp,ra,dec,know diff --git a/utils/Continuum/FunNorm.c b/utils/Continuum/FunNorm.c index da9d5df..175c82b 100644 --- a/utils/Continuum/FunNorm.c +++ b/utils/Continuum/FunNorm.c @@ -5,106 +5,106 @@ static PyObject *FunNorm_Rell(PyObject *self,PyObject *args){ -int i,j,n_datos,u=1,fin; -double pref,prel,postl,postf,m,n; + int i,j,n_datos,u=1,fin; + double pref,prel,postl,postf,m,n; -double *CLam; -PyObject *PyLam; + double *CLam; + PyObject *PyLam; -double *CFlux; -PyObject *PyFlux; + double *CFlux; + PyObject *PyFlux; -PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); -CLam = ARRAYD(PyLam); -CFlux = ARRAYD(PyFlux); + PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); + CLam = ARRAYD(PyLam); + CFlux = ARRAYD(PyFlux); -if(CFlux[n_datos-1]==-1){ - j=n_datos-2; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[n_datos-1]=CFlux[j]; - break; - } - j=j-1; + if(CFlux[n_datos-1]==-1){ + j=n_datos-2; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[n_datos-1]=CFlux[j]; + break; + } + j=j-1; - } - } + } + } -if(CFlux[0]==-1){ - j=0; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[0]=CFlux[j]; - break; - } - j++; + if(CFlux[0]==-1){ + j=0; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[0]=CFlux[j]; + break; + } + j++; - } - } + } + } -i=0; -double* theArray; -theArray = (double*) malloc((n_datos)*sizeof(double)); -while(i0){ - pref=CFlux[i-1]; - prel=CLam[i-1]; + i=0; + double* theArray; + theArray = (double*) malloc((n_datos)*sizeof(double)); + while(i0){ + pref=CFlux[i-1]; + prel=CLam[i-1]; - } - if(CFlux[i+1]>0){ - postf=CFlux[i+1]; - postl=CLam[i+1]; - m=(postf-pref)/(postl-prel); - n=postf-m*postl; - u=0; - fin=i+1; - } - i=i+1; - - } - else{ - u=0; - } - } - i=j; - if(CFlux[i]<0){ - while(i0){ + postf=CFlux[i+1]; + postl=CLam[i+1]; + m=(postf-pref)/(postl-prel); + n=postf-m*postl; + u=0; + fin=i+1; } - i=j; - theArray[i] = CFlux[i]; - i++; -} - -PyObject *lst = PyList_New(n_datos); -if(!lst) -return NULL; -for(i=0;i= 3 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "FunNorm", + NULL, + 0, //no state, so re-initialization is fine + FunNormMethods, + NULL, + NULL, + NULL, + NULL }; +PyMODINIT_FUNC PyInit_FunNorm(void) +{ + PyObject* module = PyModule_Create(&moduledef); + return module; +} + +#else void initFunNorm(void){ -(void) Py_InitModule("FunNorm", FunNormMethods); + (void) Py_InitModule("FunNorm", FunNormMethods); } +#endif + diff --git a/utils/Continuum/NewFunNorm.c b/utils/Continuum/NewFunNorm.c index 542d7d6..0c8267b 100644 --- a/utils/Continuum/NewFunNorm.c +++ b/utils/Continuum/NewFunNorm.c @@ -5,106 +5,106 @@ static PyObject *FunNorm_Rell(PyObject *self,PyObject *args){ -int i,j,n_datos,u=1,fin; -double pref,prel,postl,postf,m,n; + int i,j,n_datos,u=1,fin; + double pref,prel,postl,postf,m,n; -double *CLam; -PyObject *PyLam; + double *CLam; + PyObject *PyLam; -double *CFlux; -PyObject *PyFlux; + double *CFlux; + PyObject *PyFlux; -PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); -CLam = ARRAYD(PyLam); -CFlux = ARRAYD(PyFlux); + PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); + CLam = ARRAYD(PyLam); + CFlux = ARRAYD(PyFlux); -PyObject *myList = PyList_New(n_datos); + PyObject *myList = PyList_New(n_datos); -if(CFlux[n_datos-1]==-1){ - j=n_datos-2; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[n_datos-1]=CFlux[j]; - break; - } - j=j-1; + if(CFlux[n_datos-1]==-1){ + j=n_datos-2; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[n_datos-1]=CFlux[j]; + break; + } + j=j-1; - } - } + } + } -if(CFlux[0]==-1){ - j=0; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[0]=CFlux[j]; - break; - } - j++; + if(CFlux[0]==-1){ + j=0; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[0]=CFlux[j]; + break; + } + j++; - } - } + } + } -i=0; -double* theArray; -theArray = (double*) malloc((n_datos)*sizeof(double)); -while(i0){ - pref=CFlux[i-1]; - prel=CLam[i-1]; + i=0; + double* theArray; + theArray = (double*) malloc((n_datos)*sizeof(double)); + while(i0){ + pref=CFlux[i-1]; + prel=CLam[i-1]; - } - if(CFlux[i+1]>0){ - postf=CFlux[i+1]; - postl=CLam[i+1]; - m=(postf-pref)/(postl-prel); - n=postf-m*postl; - u=0; - fin=i+1; - } - i=i+1; - - } - else{ - u=0; - } - } - i=j; - if(CFlux[i]<0){ - while(i0){ + postf=CFlux[i+1]; + postl=CLam[i+1]; + m=(postf-pref)/(postl-prel); + n=postf-m*postl; + u=0; + fin=i+1; } - i=j; - theArray[i] = CFlux[i]; - i++; -} - -for(i=0;i= 3 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "FunNorm", + NULL, + 0, //no state, so re-initialization is fine + FunNormMethods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC PyInit_FunNorm(void) +{ + PyObject* module = PyModule_Create(&moduledef); + return module; } +#else +void initFunNorm(void){ + (void) Py_InitModule("FunNorm", FunNormMethods); +} +#endif diff --git a/utils/Continuum/contest.py b/utils/Continuum/contest.py index 0526e2f..b9174df 100644 --- a/utils/Continuum/contest.py +++ b/utils/Continuum/contest.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import numpy #import FunNorm @@ -7,280 +8,280 @@ def divis(Lx,Fx,nx): - - lt = int(len(Lx)/float(nx)) - vecA,vecB = [],[] - - for i in range(lt): - if i < lt-1: - vecA.append(numpy.mean(Lx[int(i*nx):int((i+1)*nx)])) - vecB.append(numpy.median(Fx[int(i*nx):int((i+1)*nx)])) - else: - vecA.append(numpy.mean(Lx[int(i*nx):])) - vecB.append(numpy.median(Fx[int(i*nx):])) - vecA = numpy.array(vecA) - vecB = numpy.array(vecB) + lt = int(len(Lx)/float(nx)) + vecA,vecB = [],[] - if len(vecA) > 2: - ajuste = numpy.polyfit(vecA,vecB,1) - else: - ajuste = numpy.polyval(Lx,Fx,1) + for i in range(lt): - vecA = None - vecB = None - Lx = None - Fx = None + if i < lt-1: + vecA.append(numpy.mean(Lx[int(i*nx):int((i+1)*nx)])) + vecB.append(numpy.median(Fx[int(i*nx):int((i+1)*nx)])) + else: + vecA.append(numpy.mean(Lx[int(i*nx):])) + vecB.append(numpy.median(Fx[int(i*nx):])) + vecA = numpy.array(vecA) + vecB = numpy.array(vecB) - return ajuste + if len(vecA) > 2: + ajuste = numpy.polyfit(vecA,vecB,1) + else: + ajuste = numpy.polyval(Lx,Fx,1) + + vecA = None + vecB = None + Lx = None + Fx = None + + return ajuste def NORM_single(L, F, orden = 1): - ajustep = numpy.polyfit(L,F,orden) - - - pixeles = L.shape[0] - guess = [-0.1,1.0] - guess2 = [1,0.1,0.1,0.1,0.1] - - div = 4 - rec = 20 - nnn = orden - para = numpy.zeros( (2,div),float ) - #strli,strlf = numpy.loadtxt('/data/echelle/ecpipe/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - strli = numpy.array([3965.0,3930.0,5164.0]) - strlf = numpy.array([3975.0,3940.0,5191.0]) - - #Lmien = numpy.zeros(int(pixeles/5.),float) - #Fmien = numpy.zeros(int(pixeles/5.),float) - - """ - elemino flujos nulos - """ - - I = numpy.where( F != 0.0)[0] - Llimpio = L[I] - Flimpio1 = F[I] - - """ - calculo mediana con 5 pixeles - """ - """ - i = 0 - k = 0 - - largo_nul = Llimpio.shape[0] - - while i < largo_nul: - - if i+9 < largo_nul: - Lmien[k] = numpy.mean(Llimpio[i:i+5]) - Fmien[k] = numpy.median(Flimpio[i:i+5]) - i = i+5 - k = k+1 - else: - Lmien[k] = numpy.mean(Llimpio[i:largo_nul]) - Fmien[k] = numpy.median(Flimpio[i:largo_nul]) - i = i+9 - k = k+1 - - print k, Lmien.shape - Llimpio = Lmien - Flimpio = Fmien - """ - Flimpio = scipy.signal.medfilt(Flimpio1,5) - - - - """ - no considero strong lines: - """ - - rellenado = False - - j = 0 - while j strli[j]) & (Llimpio < strlf[j]) )[0] - Flimpio[I]=-1 - - if I.shape[0]>0: - rellenado == True - - j=j+1 - - """ - completo flujo de strong lines con ajuste lineal - """ - - if rellenado: - print '!!!!!!!!!!!!!!!!!!!' - Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) - print sys.getrefcount(Flimpio) - - """ - divido cada orden en 3 y ajusto una recta - """ - - can = int(Llimpio.shape[0]/float(div)) - mm,nn=[],[] - for c in range(div): - para = divis( Llimpio[int(c*can):int((c+1)*can)], Flimpio[int(c*can):int((c+1)*can)], rec ) - #para = [1,1] - mm.append(para[0]) - nn.append(para[1]) - para = None - mm = numpy.array(mm) - nn = numpy.array(nn) - - """ - c = 0 - FF = [] - LL = [] - - while c < div: - - m = mm[c] - n = nn[c] - - if c != div-1: - segl = Llimpio[c*can:(c+1)*can] - segf = segl*m+n - elif c == div-1: - segl = Llimpio[c*can:Llimpio.shape[0]] - segf = segl*m+n - - LL = LL+list(segl) - FF = FF+list(segf) - c = c+1 - - LL = numpy.array(LL) - FF = numpy.array(FF) - ajustep = numpy.polyfit(LL,FF,nnn) - FF = numpy.polyval(ajustep,LL) - - JJ = FF.copy() - RES = Flimpio-FF - veva = numpy.zeros(pixeles,float) - AJ = numpy.zeros((Flimpio.shape[0]),float) - - WI = numpy.where(RES < 0.0)[0] - devm = numpy.sqrt(numpy.mean(RES[WI]*RES[WI])) - - I = numpy.where(RES > 0.0)[0] - veva = RES[I] - - dev = numpy.sqrt(numpy.mean(veva*veva)) - - i=0 - while i -devm and RES[i] <= 4*dev: - AJ[i] = Flimpio[i] - elif RES[i] > 4*dev: - AJ[i] = -1 - Flimpio[i] = FF[i] - else: - AJ[i] = -1 - - i=i+1 - - JJ = FF+dev - - if AJ[0 ]== -1: - AJ[0] = numpy.median(Flimpio[0:20]) - if AJ[-1] == -1: - AJ[-1] = numpy.median(Flimpio[-21:-1]) - - FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) - - LLL = Llimpio.copy() - ajustep = numpy.polyfit(LLL,FFF,nnn) - CON = numpy.polyval(ajustep,LLL) - """ - """ - itero para subir el continuo - """ - """ - j = 0 - while j < 10: - - - RESI = Flimpio-CON - larg = Flimpio.shape[0] - - loco = Llimpio.copy() - foco = Flimpio.copy() - - I = numpy.where(RESI > 0.0) - CON[I] = foco[I] - - ajustec = numpy.polyfit(loco,CON,nnn) - aji = numpy.polyval(ajustec,loco) - - CON = aji.copy() - - - j=j+1 - I = numpy.where( (Llimpio > Llimpio[0]+5.0) & (Llimpio < Llimpio[0]+10.0) )[0] - if len(I) > 2: - ajl = numpy.polyfit(Llimpio[I],aji[I],1) - pendi = ajl[0] - coefi = aji[I[0]]-pendi*Llimpio[I[0]] - aj2 = aji.copy() - aj2[0:I[0]] = Llimpio[0:I[0]]*pendi + coefi - else: - aj2 = aji.copy() - - I = numpy.where( (Llimpio > Llimpio[-1]-10.0) & (Llimpio < Llimpio[-1]-5.0) )[0] - if len(I) > 2: - ajl = numpy.polyfit(Llimpio[I],aji[I],1) - pendi = ajl[0] - coefi = aji[I[-1]]-pendi*Llimpio[I[-1]] - aj2[I[-1]:] = Llimpio[I[-1]:]*pendi + coefi - else: - aj2 = aji.copy() - ajustec = numpy.polyfit(Llimpio,aj2,nnn) - ajf = numpy.polyval(ajustec,L) - - NF = F/ajf - - para = numpy.empty(0) - strli = numpy.empty(0) - strlf = numpy.empty(0) - unos = numpy.empty(0) - Lmien = numpy.empty(0) - Fmien = numpy.empty(0) - I = numpy.empty(0) - Llimpio = numpy.empty(0) - Flimpio = numpy.empty(0) - mm = numpy.empty(0) - nn = numpy.empty(0) - vectorL = numpy.empty(0) - vectorF = numpy.empty(0) - segl = numpy.empty(0) - segf = numpy.empty(0) - LL = numpy.empty(0) - FF = numpy.empty(0) - JJ = numpy.empty(0) - RES = numpy.empty(0) - veva = numpy.empty(0) - AJ = numpy.empty(0) - WI = numpy.empty(0) - FFF = numpy.empty(0) - LLL = numpy.empty(0) - CON = numpy.empty(0) - RESI = numpy.empty(0) - loco = numpy.empty(0) - foco = numpy.empty(0) - aji = numpy.empty(0) - coefi = numpy.empty(0) - aj2 = numpy.empty(0) - ajustec = numpy.empty(0) - ajf = numpy.empty(0) - L = numpy.empty(0) - F = numpy.empty(0) - gc.collect() - """ - gc.collect() - return ajustep + ajustep = numpy.polyfit(L,F,orden) + + + pixeles = L.shape[0] + guess = [-0.1,1.0] + guess2 = [1,0.1,0.1,0.1,0.1] + + div = 4 + rec = 20 + nnn = orden + para = numpy.zeros( (2,div),float ) + #strli,strlf = numpy.loadtxt('/data/echelle/ecpipe/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + strli = numpy.array([3965.0,3930.0,5164.0]) + strlf = numpy.array([3975.0,3940.0,5191.0]) + + #Lmien = numpy.zeros(int(pixeles/5.),float) + #Fmien = numpy.zeros(int(pixeles/5.),float) + + """ + elemino flujos nulos + """ + + I = numpy.where( F != 0.0)[0] + Llimpio = L[I] + Flimpio1 = F[I] + + """ + calculo mediana con 5 pixeles + """ + """ + i = 0 + k = 0 + + largo_nul = Llimpio.shape[0] + + while i < largo_nul: + + if i+9 < largo_nul: + Lmien[k] = numpy.mean(Llimpio[i:i+5]) + Fmien[k] = numpy.median(Flimpio[i:i+5]) + i = i+5 + k = k+1 + else: + Lmien[k] = numpy.mean(Llimpio[i:largo_nul]) + Fmien[k] = numpy.median(Flimpio[i:largo_nul]) + i = i+9 + k = k+1 + + print k, Lmien.shape + Llimpio = Lmien + Flimpio = Fmien + """ + Flimpio = scipy.signal.medfilt(Flimpio1,5) + + + + """ + no considero strong lines: + """ + + rellenado = False + + j = 0 + while j strli[j]) & (Llimpio < strlf[j]) )[0] + Flimpio[I]=-1 + + if I.shape[0]>0: + rellenado == True + + j=j+1 + + """ + completo flujo de strong lines con ajuste lineal + """ + + if rellenado: + print('!!!!!!!!!!!!!!!!!!!') + Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) + print(sys.getrefcount(Flimpio)) + + """ + divido cada orden en 3 y ajusto una recta + """ + + can = int(Llimpio.shape[0]/float(div)) + mm,nn=[],[] + for c in range(div): + para = divis( Llimpio[int(c*can):int((c+1)*can)], Flimpio[int(c*can):int((c+1)*can)], rec ) + #para = [1,1] + mm.append(para[0]) + nn.append(para[1]) + para = None + mm = numpy.array(mm) + nn = numpy.array(nn) + + """ + c = 0 + FF = [] + LL = [] + + while c < div: + + m = mm[c] + n = nn[c] + + if c != div-1: + segl = Llimpio[c*can:(c+1)*can] + segf = segl*m+n + elif c == div-1: + segl = Llimpio[c*can:Llimpio.shape[0]] + segf = segl*m+n + + LL = LL+list(segl) + FF = FF+list(segf) + c = c+1 + + LL = numpy.array(LL) + FF = numpy.array(FF) + ajustep = numpy.polyfit(LL,FF,nnn) + FF = numpy.polyval(ajustep,LL) + + JJ = FF.copy() + RES = Flimpio-FF + veva = numpy.zeros(pixeles,float) + AJ = numpy.zeros((Flimpio.shape[0]),float) + + WI = numpy.where(RES < 0.0)[0] + devm = numpy.sqrt(numpy.mean(RES[WI]*RES[WI])) + + I = numpy.where(RES > 0.0)[0] + veva = RES[I] + + dev = numpy.sqrt(numpy.mean(veva*veva)) + + i=0 + while i -devm and RES[i] <= 4*dev: + AJ[i] = Flimpio[i] + elif RES[i] > 4*dev: + AJ[i] = -1 + Flimpio[i] = FF[i] + else: + AJ[i] = -1 + + i=i+1 + + JJ = FF+dev + + if AJ[0 ]== -1: + AJ[0] = numpy.median(Flimpio[0:20]) + if AJ[-1] == -1: + AJ[-1] = numpy.median(Flimpio[-21:-1]) + + FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) + + LLL = Llimpio.copy() + ajustep = numpy.polyfit(LLL,FFF,nnn) + CON = numpy.polyval(ajustep,LLL) + """ + """ + itero para subir el continuo + """ + """ + j = 0 + while j < 10: + + + RESI = Flimpio-CON + larg = Flimpio.shape[0] + + loco = Llimpio.copy() + foco = Flimpio.copy() + + I = numpy.where(RESI > 0.0) + CON[I] = foco[I] + + ajustec = numpy.polyfit(loco,CON,nnn) + aji = numpy.polyval(ajustec,loco) + + CON = aji.copy() + + + j=j+1 + I = numpy.where( (Llimpio > Llimpio[0]+5.0) & (Llimpio < Llimpio[0]+10.0) )[0] + if len(I) > 2: + ajl = numpy.polyfit(Llimpio[I],aji[I],1) + pendi = ajl[0] + coefi = aji[I[0]]-pendi*Llimpio[I[0]] + aj2 = aji.copy() + aj2[0:I[0]] = Llimpio[0:I[0]]*pendi + coefi + else: + aj2 = aji.copy() + + I = numpy.where( (Llimpio > Llimpio[-1]-10.0) & (Llimpio < Llimpio[-1]-5.0) )[0] + if len(I) > 2: + ajl = numpy.polyfit(Llimpio[I],aji[I],1) + pendi = ajl[0] + coefi = aji[I[-1]]-pendi*Llimpio[I[-1]] + aj2[I[-1]:] = Llimpio[I[-1]:]*pendi + coefi + else: + aj2 = aji.copy() + ajustec = numpy.polyfit(Llimpio,aj2,nnn) + ajf = numpy.polyval(ajustec,L) + + NF = F/ajf + + para = numpy.empty(0) + strli = numpy.empty(0) + strlf = numpy.empty(0) + unos = numpy.empty(0) + Lmien = numpy.empty(0) + Fmien = numpy.empty(0) + I = numpy.empty(0) + Llimpio = numpy.empty(0) + Flimpio = numpy.empty(0) + mm = numpy.empty(0) + nn = numpy.empty(0) + vectorL = numpy.empty(0) + vectorF = numpy.empty(0) + segl = numpy.empty(0) + segf = numpy.empty(0) + LL = numpy.empty(0) + FF = numpy.empty(0) + JJ = numpy.empty(0) + RES = numpy.empty(0) + veva = numpy.empty(0) + AJ = numpy.empty(0) + WI = numpy.empty(0) + FFF = numpy.empty(0) + LLL = numpy.empty(0) + CON = numpy.empty(0) + RESI = numpy.empty(0) + loco = numpy.empty(0) + foco = numpy.empty(0) + aji = numpy.empty(0) + coefi = numpy.empty(0) + aj2 = numpy.empty(0) + ajustec = numpy.empty(0) + ajf = numpy.empty(0) + L = numpy.empty(0) + F = numpy.empty(0) + gc.collect() + """ + gc.collect() + return ajustep diff --git a/utils/Continuum/continuum.py b/utils/Continuum/continuum.py index a84c9f8..62b6a8e 100644 --- a/utils/Continuum/continuum.py +++ b/utils/Continuum/continuum.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import math from astropy.io import fits as pyfits @@ -13,1088 +14,1088 @@ def division(L,F,n): - x = int(len(L)/n) - vec = numpy.zeros((2,x),float) + x = int(len(L)/n) + vec = numpy.zeros((2,x),float) - i = 0 - while i < x: + i = 0 + while i < x: - if i < x-1: - vec[0][i] = numpy.mean(L[i*n:(i+1)*n]) - vec[1][i] = numpy.median(F[i*n:(i+1)*n]) - elif i == x-1: - vec[0][i] = numpy.mean(L[i*n:len(L)]) - vec[1][i] = numpy.median(F[i*n:len(F)]) - - i = i+1 - - g = [-0.1,1.0] - if vec.shape[1] > 2: - ajuste = optimize.leastsq(res_lin,g,args=(vec[1][:],vec[0][:])) + if i < x-1: + vec[0][i] = numpy.mean(L[i*n:(i+1)*n]) + vec[1][i] = numpy.median(F[i*n:(i+1)*n]) + elif i == x-1: + vec[0][i] = numpy.mean(L[i*n:len(L)]) + vec[1][i] = numpy.median(F[i*n:len(F)]) - else: - ajuste = optimize.leastsq(res_lin,g,args=(F,L)) + i = i+1 - return ajuste + g = [-0.1,1.0] + if vec.shape[1] > 2: + ajuste = optimize.leastsq(res_lin,g,args=(vec[1][:],vec[0][:])) + + else: + ajuste = optimize.leastsq(res_lin,g,args=(F,L)) + + return ajuste def lin(params,x): - m = params[0] - n = params[1] - y = m*x+n - return y + m = params[0] + n = params[1] + y = m*x+n + return y def res_lin(params,y,x): - return y-lin(params,x) + return y-lin(params,x) def curva(params,x): - a_0=params[0] - a_1=params[1] - a_2=params[2] - a_3=params[3] - a_4=params[4] - - y=a_0+a_1*x+a_2*x*x+a_3*(x**3)+a_4*(x**4) - return y + a_0=params[0] + a_1=params[1] + a_2=params[2] + a_3=params[3] + a_4=params[4] + + y=a_0+a_1*x+a_2*x*x+a_3*(x**3)+a_4*(x**4) + return y def res_cur(params,y,x): - return y-curva(params,x) + return y-curva(params,x) def NORM(L,F,orden=2): - ordenes = L.shape[0] - pixeles = L.shape[1] - - guess = [-0.1,1.0] - guess2 = [1,0.1,0.1,0.1,0.1] - - div = 4 - rec = 20 - - para = numpy.zeros( (2,div),float ) - continuo = numpy.zeros( (2,ordenes),list ) - strl = numpy.zeros( (6,2),float ) - laro = numpy.zeros( ordenes,int ) - - #strli,strlf = numpy.loadtxt('/home/rabrahm/Desktop/corr2/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - strli,strlf = numpy.loadtxt('../utils/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - - esto = numpy.zeros( (2,ordenes),list ) - ajent = numpy.zeros( (ordenes,pixeles),float ) - unos = numpy.zeros(pixeles,float) - NORMALIZADO = numpy.zeros(ordenes,list) - unos = unos+1.0 - - Lfilter = numpy.zeros(ordenes,list) - Ffilter = numpy.zeros(ordenes,list) - coefs = numpy.zeros([ordenes,orden+1]) - - o=0 - while o strli[j]) & (Llimpio < strlf[j]) )[0] - Flimpio[I]=-1 - - if I.shape[0] > 0: - rellenado == True - - j=j+1 - - """ - completo flujo de strong lines con ajuste lineal - """ - if rellenado: - Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) - - """ - divido cada orden en 3 y ajusto una recta - """ - - can = int(Llimpio.shape[0]/div) - mm = numpy.zeros(div,float) - nn = numpy.zeros(div,float) - - c=0 - while c < div: - - vectorL = Llimpio[c*can:(c+1)*can] - vectorF = Flimpio[c*can:(c+1)*can] - para = division(vectorL,vectorF,rec) - - mm[c] = para[0][0] - nn[c] = para[0][1] - c = c+1 - - c = 0 - FF = [] - LL = [] - - while c < div: - - m = mm[c] - n = nn[c] - - if c != div-1: - segl = Llimpio[c*can:(c+1)*can] - segf = segl*m+n - elif c == div-1: - segl = Llimpio[c*can:Llimpio.shape[0]] - segf = segl*m+n - - LL = LL+list(segl) - FF = FF+list(segf) - c = c+1 - - LL = numpy.array(LL) - FF = numpy.array(FF) - - ajustep = numpy.polyfit(LL,FF,orden) - FF = numpy.polyfit(ajustep, LL) - """ - ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) - a0=ajustep[0][0] - a1=ajustep[0][1] - a2=ajustep[0][2] - a3=ajustep[0][3] - a4=ajustep[0][4] - - FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) - """ - """ - considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 - """ - - JJ = FF.copy() - RES = Flimpio-FF - veva = numpy.zeros(pixeles,float) - AJ = numpy.zeros((Flimpio.shape[0]),float) - - I = numpy.where(RES > 0.0)[0] - veva = RES[I] - - dev = math.sqrt(numpy.mean(veva*veva)) - - i=0 - while i dev and RES[i] <= 4*dev: - AJ[i] = Flimpio[i] - elif RES[i] > 4*dev: - AJ[i] = -1 - Flimpio[i] = FF[i] - else: - AJ[i] = -1 - - i=i+1 - - JJ = FF+dev - - if AJ[0]== -1: - AJ[0] = numpy.median(Flimpio[0:20]) - if AJ[-1] == -1: - AJ[-1] = numpy.median(Flimpio[-21:-1]) - - FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) - LLL = Llimpio.copy() - - ajustep = numpy.polyfit(LLL,FFF,orden) - CON = numpy.polyfit(ajustep, LLL) - """ - ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) - a0 = ajustep[0][0] - a1 = ajustep[0][1] - a2 = ajustep[0][2] - a3 = ajustep[0][3] - a4 = ajustep[0][4] - - CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) - """ - continuo[0,o] = list(LLL) - continuo[1,o] = list(CON) - - laro[o] = LLL.shape[0] - - esto[0,o] = list(Llimpio) - esto[1,o] = list(Flimpio) - - o=o+1 - - """ - itero para subir el continuo - """ - - #print "iterando para subir el continuo" - - j = 0 - while j < 20: - - o = 0 - while o < ordenes: - - ya = numpy.array(continuo[1,o]) - RESI = numpy.array(esto[1,o])-ya - larg = laro[o] - - loco=numpy.array(esto[0,o]) - foco=numpy.array(esto[1,o]) - - I = numpy.where(RESI > 0)[0] - - ya[I] = foco[I] - - - ajustec = numpy.polyfit(loco,ya) - aji = numpy.polyval(ajustec,loco[0:larg]) - invor = numpy.arange(orden+1)[::-1] - for orde in invor: - coefs[o,orde] = invor[orde] - """ - ajustec = optimize.leastsq(res_cur,guess2,args=(ya,loco)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - coefs[o,0]=A0 - coefs[o,1]=A1 - coefs[o,2]=A2 - coefs[o,3]=A3 - coefs[o,4]=A4 - aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) - """ - continuo[1,o] = list(aji) - - o=o+1 - j=j+1 - - - - """ - perfecciono bordes del ajuste con pendiente de orden anterior y posterior - """ - - #print "arreglando bordes de cada orden" - - o = 0 - while o < ordenes-1: - - larg = laro[o] - longitud = numpy.array(continuo[0,o]) - upL = numpy.array(continuo[0,o+1]) - upF = numpy.array(continuo[1,o+1]) - - lami = longitud[0] - lamf = lami+5 - - I = numpy.where( (upL >= lami) & (upL <= lamf))[0] - - if len(I)>4: - - veL = upL[I] - veF = upF[I] - - ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) - pen = ajuster[0][0] - - j = 0 - - while j < 5: - - flujo = numpy.array(continuo[1,o]) - I = numpy.where( longitud >= lamf)[0][0] - x = longitud[I] - y = flujo[I] - - coe = y-pen*x - - flujo[0:I] = longitud[0:I]*pen+coe - - ajustec = numpy.polyfit(longitud,flujo,orden) - tri = numpy.polyval(ajustec, longitud) - - """ - ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - - tri=A0+A1*longitud+A2*(longitud**2)+A3*(longitud**3)+A4*(longitud**4) - """ - continuo[1,o] = list(tri) - - j = j+1 - - - if o == 0: - - AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - else: - AJUSTE_FINAL = numpy.polyval(coefs[o],L[o]) - #AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - o=o+1 - - o = ordenes-1 - while o > 0: - - longitud=numpy.array(continuo[0,o]) - downL=numpy.array(continuo[0,o-1]) - downF=numpy.array(continuo[1,o-1]) - larg=laro[o] - lamf=longitud[larg-1] - lami=lamf-5 - - I = numpy.where((downL>=lami) & (downL<=lamf))[0] - if len(I)>4: - veL = downL[I] - veF = downF[I] - - ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) - pen = ajuster[0][0] - - j = 0 - while j < 3: - - flujo = numpy.array(continuo[1,o]) - I = numpy.where(longitud<=lami)[0][-1] - x = longitud[I] - y = flujo[I] - - coe = y-pen*x - - flujo[I:] = longitud[I:]*pen+coe - ajustec = numpy.polyfit(longitud,flujo,orden) - tri = numpy.polyval(ajustec, longitud) - """ - ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - - tri=A0+A1*(longitud[:]**1)+A2*(longitud[:]**2)+A3*(longitud[:]**3)+A4*(longitud[:]**4) - """ - continuo[1,o]=list(tri) - - j=j+1 - AJUSTE_FINAL = numpy.polyval( ajustec,L[o] ) - #AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) - - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - else: - AJUSTE_FINAL = numpy.polyval(coefs[o],L[o]) - #AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - o=o-1 - - AAAA = numpy.zeros(ordenes,list) - BBBB = numpy.zeros(ordenes,list) - - o = 0 - while o < ordenes: - - AAAA[o] = numpy.array(NORMALIZADO[o]) - BBBB[o]=L[o] - - o = o+1; - - return BBBB,AAAA; + ordenes = L.shape[0] + pixeles = L.shape[1] + + guess = [-0.1,1.0] + guess2 = [1,0.1,0.1,0.1,0.1] + + div = 4 + rec = 20 + + para = numpy.zeros( (2,div),float ) + continuo = numpy.zeros( (2,ordenes),list ) + strl = numpy.zeros( (6,2),float ) + laro = numpy.zeros( ordenes,int ) + + #strli,strlf = numpy.loadtxt('/home/rabrahm/Desktop/corr2/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + strli,strlf = numpy.loadtxt('../utils/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + + esto = numpy.zeros( (2,ordenes),list ) + ajent = numpy.zeros( (ordenes,pixeles),float ) + unos = numpy.zeros(pixeles,float) + NORMALIZADO = numpy.zeros(ordenes,list) + unos = unos+1.0 + + Lfilter = numpy.zeros(ordenes,list) + Ffilter = numpy.zeros(ordenes,list) + coefs = numpy.zeros([ordenes,orden+1]) + + o=0 + while o strli[j]) & (Llimpio < strlf[j]) )[0] + Flimpio[I]=-1 + + if I.shape[0] > 0: + rellenado == True + + j=j+1 + + """ + completo flujo de strong lines con ajuste lineal + """ + if rellenado: + Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) + + """ + divido cada orden en 3 y ajusto una recta + """ + + can = int(Llimpio.shape[0]/div) + mm = numpy.zeros(div,float) + nn = numpy.zeros(div,float) + + c=0 + while c < div: + + vectorL = Llimpio[c*can:(c+1)*can] + vectorF = Flimpio[c*can:(c+1)*can] + para = division(vectorL,vectorF,rec) + + mm[c] = para[0][0] + nn[c] = para[0][1] + c = c+1 + + c = 0 + FF = [] + LL = [] + + while c < div: + + m = mm[c] + n = nn[c] + + if c != div-1: + segl = Llimpio[c*can:(c+1)*can] + segf = segl*m+n + elif c == div-1: + segl = Llimpio[c*can:Llimpio.shape[0]] + segf = segl*m+n + + LL = LL+list(segl) + FF = FF+list(segf) + c = c+1 + + LL = numpy.array(LL) + FF = numpy.array(FF) + + ajustep = numpy.polyfit(LL,FF,orden) + FF = numpy.polyfit(ajustep, LL) + """ + ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) + a0=ajustep[0][0] + a1=ajustep[0][1] + a2=ajustep[0][2] + a3=ajustep[0][3] + a4=ajustep[0][4] + + FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) + """ + """ + considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 + """ + + JJ = FF.copy() + RES = Flimpio-FF + veva = numpy.zeros(pixeles,float) + AJ = numpy.zeros((Flimpio.shape[0]),float) + + I = numpy.where(RES > 0.0)[0] + veva = RES[I] + + dev = math.sqrt(numpy.mean(veva*veva)) + + i=0 + while i dev and RES[i] <= 4*dev: + AJ[i] = Flimpio[i] + elif RES[i] > 4*dev: + AJ[i] = -1 + Flimpio[i] = FF[i] + else: + AJ[i] = -1 + + i=i+1 + + JJ = FF+dev + + if AJ[0]== -1: + AJ[0] = numpy.median(Flimpio[0:20]) + if AJ[-1] == -1: + AJ[-1] = numpy.median(Flimpio[-21:-1]) + + FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) + LLL = Llimpio.copy() + + ajustep = numpy.polyfit(LLL,FFF,orden) + CON = numpy.polyfit(ajustep, LLL) + """ + ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) + a0 = ajustep[0][0] + a1 = ajustep[0][1] + a2 = ajustep[0][2] + a3 = ajustep[0][3] + a4 = ajustep[0][4] + + CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) + """ + continuo[0,o] = list(LLL) + continuo[1,o] = list(CON) + + laro[o] = LLL.shape[0] + + esto[0,o] = list(Llimpio) + esto[1,o] = list(Flimpio) + + o=o+1 + + """ + itero para subir el continuo + """ + + #print "iterando para subir el continuo" + + j = 0 + while j < 20: + + o = 0 + while o < ordenes: + + ya = numpy.array(continuo[1,o]) + RESI = numpy.array(esto[1,o])-ya + larg = laro[o] + + loco=numpy.array(esto[0,o]) + foco=numpy.array(esto[1,o]) + + I = numpy.where(RESI > 0)[0] + + ya[I] = foco[I] + + + ajustec = numpy.polyfit(loco,ya) + aji = numpy.polyval(ajustec,loco[0:larg]) + invor = numpy.arange(orden+1)[::-1] + for orde in invor: + coefs[o,orde] = invor[orde] + """ + ajustec = optimize.leastsq(res_cur,guess2,args=(ya,loco)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + coefs[o,0]=A0 + coefs[o,1]=A1 + coefs[o,2]=A2 + coefs[o,3]=A3 + coefs[o,4]=A4 + aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) + """ + continuo[1,o] = list(aji) + + o=o+1 + j=j+1 + + + + """ + perfecciono bordes del ajuste con pendiente de orden anterior y posterior + """ + + #print "arreglando bordes de cada orden" + + o = 0 + while o < ordenes-1: + + larg = laro[o] + longitud = numpy.array(continuo[0,o]) + upL = numpy.array(continuo[0,o+1]) + upF = numpy.array(continuo[1,o+1]) + + lami = longitud[0] + lamf = lami+5 + + I = numpy.where( (upL >= lami) & (upL <= lamf))[0] + + if len(I)>4: + + veL = upL[I] + veF = upF[I] + + ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) + pen = ajuster[0][0] + + j = 0 + + while j < 5: + + flujo = numpy.array(continuo[1,o]) + I = numpy.where( longitud >= lamf)[0][0] + x = longitud[I] + y = flujo[I] + + coe = y-pen*x + + flujo[0:I] = longitud[0:I]*pen+coe + + ajustec = numpy.polyfit(longitud,flujo,orden) + tri = numpy.polyval(ajustec, longitud) + + """ + ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + + tri=A0+A1*longitud+A2*(longitud**2)+A3*(longitud**3)+A4*(longitud**4) + """ + continuo[1,o] = list(tri) + + j = j+1 + + + if o == 0: + + AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + else: + AJUSTE_FINAL = numpy.polyval(coefs[o],L[o]) + #AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + o=o+1 + + o = ordenes-1 + while o > 0: + + longitud=numpy.array(continuo[0,o]) + downL=numpy.array(continuo[0,o-1]) + downF=numpy.array(continuo[1,o-1]) + larg=laro[o] + lamf=longitud[larg-1] + lami=lamf-5 + + I = numpy.where((downL>=lami) & (downL<=lamf))[0] + if len(I)>4: + veL = downL[I] + veF = downF[I] + + ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) + pen = ajuster[0][0] + + j = 0 + while j < 3: + + flujo = numpy.array(continuo[1,o]) + I = numpy.where(longitud<=lami)[0][-1] + x = longitud[I] + y = flujo[I] + + coe = y-pen*x + + flujo[I:] = longitud[I:]*pen+coe + ajustec = numpy.polyfit(longitud,flujo,orden) + tri = numpy.polyval(ajustec, longitud) + """ + ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + + tri=A0+A1*(longitud[:]**1)+A2*(longitud[:]**2)+A3*(longitud[:]**3)+A4*(longitud[:]**4) + """ + continuo[1,o]=list(tri) + + j=j+1 + AJUSTE_FINAL = numpy.polyval( ajustec,L[o] ) + #AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) + + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + else: + AJUSTE_FINAL = numpy.polyval(coefs[o],L[o]) + #AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + o=o-1 + + AAAA = numpy.zeros(ordenes,list) + BBBB = numpy.zeros(ordenes,list) + + o = 0 + while o < ordenes: + + AAAA[o] = numpy.array(NORMALIZADO[o]) + BBBB[o]=L[o] + + o = o+1; + + return BBBB,AAAA; def NORM2(L,F): - ordenes = L.shape[0] - pixeles = L.shape[1] - - guess = [-0.1,1.0] - guess2 = [1,0.1,0.1,0.1,0.1] - - div = 4 - rec = 20 - - para = numpy.zeros( (2,div),float ) - continuo = numpy.zeros( (2,ordenes),list ) - strl = numpy.zeros( (6,2),float ) - laro = numpy.zeros( ordenes,int ) - - #strli,strlf = numpy.loadtxt('/home/rabrahm/Desktop/corr2/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - strli,strlf = numpy.loadtxt('../utils/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - - esto = numpy.zeros( (2,ordenes),list ) - ajent = numpy.zeros( (ordenes,pixeles),float ) - unos = numpy.zeros(pixeles,float) - NORMALIZADO = numpy.zeros(ordenes,list) - unos = unos+1.0 - - Lfilter = numpy.zeros(ordenes,list) - Ffilter = numpy.zeros(ordenes,list) - coefs = numpy.zeros([ordenes,5]) - - o=0 - while o strli[j]) & (Llimpio < strlf[j]) )[0] - Flimpio[I]=-1 - - if I.shape[0] > 0: - rellenado == True - - j=j+1 - - """ - completo flujo de strong lines con ajuste lineal - """ - if rellenado: - Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) - - """ - divido cada orden en 3 y ajusto una recta - """ - - can = int(Llimpio.shape[0]/div) - mm = numpy.zeros(div,float) - nn = numpy.zeros(div,float) - - c=0 - while c < div: - - vectorL = Llimpio[c*can:(c+1)*can] - vectorF = Flimpio[c*can:(c+1)*can] - para = division(vectorL,vectorF,rec) - - mm[c] = para[0][0] - nn[c] = para[0][1] - c = c+1 - - c = 0 - FF = [] - LL = [] - - while c < div: - - m = mm[c] - n = nn[c] - - if c != div-1: - segl = Llimpio[c*can:(c+1)*can] - segf = segl*m+n - elif c == div-1: - segl = Llimpio[c*can:Llimpio.shape[0]] - segf = segl*m+n - - LL = LL+list(segl) - FF = FF+list(segf) - c = c+1 - - LL = numpy.array(LL) - FF = numpy.array(FF) - - ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) - a0=ajustep[0][0] - a1=ajustep[0][1] - a2=ajustep[0][2] - a3=ajustep[0][3] - a4=ajustep[0][4] - - FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) - - """ - considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 - """ - - JJ = FF.copy() - RES = Flimpio-FF - veva = numpy.zeros(pixeles,float) - AJ = numpy.zeros((Flimpio.shape[0]),float) - - I = numpy.where(RES > 0.0)[0] - veva = RES[I] - - dev = math.sqrt(numpy.mean(veva*veva)) - - i=0 - while i dev and RES[i] <= 4*dev: - AJ[i] = Flimpio[i] - elif RES[i] > 4*dev: - AJ[i] = -1 - Flimpio[i] = FF[i] - else: - AJ[i] = -1 - - i=i+1 - - JJ = FF+dev - - if AJ[0]== -1: - AJ[0] = numpy.median(Flimpio[0:20]) - if AJ[-1] == -1: - AJ[-1] = numpy.median(Flimpio[-21:-1]) - - FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) - LLL = Llimpio.copy() - - ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) - a0 = ajustep[0][0] - a1 = ajustep[0][1] - a2 = ajustep[0][2] - a3 = ajustep[0][3] - a4 = ajustep[0][4] - - CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) - - continuo[0,o] = list(LLL) - continuo[1,o] = list(CON) - - laro[o] = LLL.shape[0] - - esto[0,o] = list(Llimpio) - esto[1,o] = list(Flimpio) - - o=o+1 - - """ - itero para subir el continuo - """ - - #print "iterando para subir el continuo" - - j = 0 - while j < 20: - - o = 0 - while o < ordenes: - - ya = numpy.array(continuo[1,o]) - RESI = numpy.array(esto[1,o])-ya - larg = laro[o] - - loco=numpy.array(esto[0,o]) - foco=numpy.array(esto[1,o]) - - I = numpy.where(RESI > 0)[0] - - ya[I] = foco[I] - - ajustec = optimize.leastsq(res_cur,guess2,args=(ya,loco)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - coefs[o,0]=A0 - coefs[o,1]=A1 - coefs[o,2]=A2 - coefs[o,3]=A3 - coefs[o,4]=A4 - aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) - - continuo[1,o] = list(aji) - - o=o+1 - j=j+1 - - - - """ - perfecciono bordes del ajuste con pendiente de orden anterior y posterior - """ - - #print "arreglando bordes de cada orden" - - o = 0 - while o < ordenes-1: - - larg = laro[o] - longitud = numpy.array(continuo[0,o]) - upL = numpy.array(continuo[0,o+1]) - upF = numpy.array(continuo[1,o+1]) - - lami = longitud[0] - lamf = lami+5 - - I = numpy.where( (upL >= lami) & (upL <= lamf))[0] - - if len(I)>4: - - veL = upL[I] - veF = upF[I] - - ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) - pen = ajuster[0][0] - - j = 0 - - while j < 5: - - flujo = numpy.array(continuo[1,o]) - I = numpy.where( longitud >= lamf)[0][0] - x = longitud[I] - y = flujo[I] - - coe = y-pen*x - - flujo[0:I] = longitud[0:I]*pen+coe - - ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - - tri=A0+A1*longitud+A2*(longitud**2)+A3*(longitud**3)+A4*(longitud**4) - - continuo[1,o] = list(tri) - - j = j+1 - - - if o == 0: - - AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - else: - AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - o=o+1 - - o = ordenes-1 - while o > 0: - - longitud=numpy.array(continuo[0,o]) - downL=numpy.array(continuo[0,o-1]) - downF=numpy.array(continuo[1,o-1]) - larg=laro[o] - lamf=longitud[larg-1] - lami=lamf-5 - - I = numpy.where((downL>=lami) & (downL<=lamf))[0] - if len(I)>4: - veL = downL[I] - veF = downF[I] - - ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) - pen = ajuster[0][0] - - j = 0 - while j < 3: - - flujo = numpy.array(continuo[1,o]) - I = numpy.where(longitud<=lami)[0][-1] - x = longitud[I] - y = flujo[I] - - coe = y-pen*x - - flujo[I:] = longitud[I:]*pen+coe - - ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - - tri=A0+A1*(longitud[:]**1)+A2*(longitud[:]**2)+A3*(longitud[:]**3)+A4*(longitud[:]**4) - continuo[1,o]=list(tri) - - j=j+1 - - AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) - - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - - else: - AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) - NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) - o=o-1 - - AAAA = numpy.zeros(ordenes,list) - BBBB = numpy.zeros(ordenes,list) - - o = 0 - while o < ordenes: - - AAAA[o] = numpy.array(NORMALIZADO[o]) - BBBB[o]=L[o] - - o = o+1; - - return BBBB,AAAA; + ordenes = L.shape[0] + pixeles = L.shape[1] + + guess = [-0.1,1.0] + guess2 = [1,0.1,0.1,0.1,0.1] + + div = 4 + rec = 20 + + para = numpy.zeros( (2,div),float ) + continuo = numpy.zeros( (2,ordenes),list ) + strl = numpy.zeros( (6,2),float ) + laro = numpy.zeros( ordenes,int ) + + #strli,strlf = numpy.loadtxt('/home/rabrahm/Desktop/corr2/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + strli,strlf = numpy.loadtxt('../utils/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + + esto = numpy.zeros( (2,ordenes),list ) + ajent = numpy.zeros( (ordenes,pixeles),float ) + unos = numpy.zeros(pixeles,float) + NORMALIZADO = numpy.zeros(ordenes,list) + unos = unos+1.0 + + Lfilter = numpy.zeros(ordenes,list) + Ffilter = numpy.zeros(ordenes,list) + coefs = numpy.zeros([ordenes,5]) + + o=0 + while o strli[j]) & (Llimpio < strlf[j]) )[0] + Flimpio[I]=-1 + + if I.shape[0] > 0: + rellenado == True + + j=j+1 + + """ + completo flujo de strong lines con ajuste lineal + """ + if rellenado: + Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) + + """ + divido cada orden en 3 y ajusto una recta + """ + + can = int(Llimpio.shape[0]/div) + mm = numpy.zeros(div,float) + nn = numpy.zeros(div,float) + + c=0 + while c < div: + + vectorL = Llimpio[c*can:(c+1)*can] + vectorF = Flimpio[c*can:(c+1)*can] + para = division(vectorL,vectorF,rec) + + mm[c] = para[0][0] + nn[c] = para[0][1] + c = c+1 + + c = 0 + FF = [] + LL = [] + + while c < div: + + m = mm[c] + n = nn[c] + + if c != div-1: + segl = Llimpio[c*can:(c+1)*can] + segf = segl*m+n + elif c == div-1: + segl = Llimpio[c*can:Llimpio.shape[0]] + segf = segl*m+n + + LL = LL+list(segl) + FF = FF+list(segf) + c = c+1 + + LL = numpy.array(LL) + FF = numpy.array(FF) + + ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) + a0=ajustep[0][0] + a1=ajustep[0][1] + a2=ajustep[0][2] + a3=ajustep[0][3] + a4=ajustep[0][4] + + FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) + + """ + considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 + """ + + JJ = FF.copy() + RES = Flimpio-FF + veva = numpy.zeros(pixeles,float) + AJ = numpy.zeros((Flimpio.shape[0]),float) + + I = numpy.where(RES > 0.0)[0] + veva = RES[I] + + dev = math.sqrt(numpy.mean(veva*veva)) + + i=0 + while i dev and RES[i] <= 4*dev: + AJ[i] = Flimpio[i] + elif RES[i] > 4*dev: + AJ[i] = -1 + Flimpio[i] = FF[i] + else: + AJ[i] = -1 + + i=i+1 + + JJ = FF+dev + + if AJ[0]== -1: + AJ[0] = numpy.median(Flimpio[0:20]) + if AJ[-1] == -1: + AJ[-1] = numpy.median(Flimpio[-21:-1]) + + FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) + LLL = Llimpio.copy() + + ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) + a0 = ajustep[0][0] + a1 = ajustep[0][1] + a2 = ajustep[0][2] + a3 = ajustep[0][3] + a4 = ajustep[0][4] + + CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) + + continuo[0,o] = list(LLL) + continuo[1,o] = list(CON) + + laro[o] = LLL.shape[0] + + esto[0,o] = list(Llimpio) + esto[1,o] = list(Flimpio) + + o=o+1 + + """ + itero para subir el continuo + """ + + #print "iterando para subir el continuo" + + j = 0 + while j < 20: + + o = 0 + while o < ordenes: + + ya = numpy.array(continuo[1,o]) + RESI = numpy.array(esto[1,o])-ya + larg = laro[o] + + loco=numpy.array(esto[0,o]) + foco=numpy.array(esto[1,o]) + + I = numpy.where(RESI > 0)[0] + + ya[I] = foco[I] + + ajustec = optimize.leastsq(res_cur,guess2,args=(ya,loco)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + coefs[o,0]=A0 + coefs[o,1]=A1 + coefs[o,2]=A2 + coefs[o,3]=A3 + coefs[o,4]=A4 + aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) + + continuo[1,o] = list(aji) + + o=o+1 + j=j+1 + + + + """ + perfecciono bordes del ajuste con pendiente de orden anterior y posterior + """ + + #print "arreglando bordes de cada orden" + + o = 0 + while o < ordenes-1: + + larg = laro[o] + longitud = numpy.array(continuo[0,o]) + upL = numpy.array(continuo[0,o+1]) + upF = numpy.array(continuo[1,o+1]) + + lami = longitud[0] + lamf = lami+5 + + I = numpy.where( (upL >= lami) & (upL <= lamf))[0] + + if len(I)>4: + + veL = upL[I] + veF = upF[I] + + ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) + pen = ajuster[0][0] + + j = 0 + + while j < 5: + + flujo = numpy.array(continuo[1,o]) + I = numpy.where( longitud >= lamf)[0][0] + x = longitud[I] + y = flujo[I] + + coe = y-pen*x + + flujo[0:I] = longitud[0:I]*pen+coe + + ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + + tri=A0+A1*longitud+A2*(longitud**2)+A3*(longitud**3)+A4*(longitud**4) + + continuo[1,o] = list(tri) + + j = j+1 + + + if o == 0: + + AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + else: + AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + o=o+1 + + o = ordenes-1 + while o > 0: + + longitud=numpy.array(continuo[0,o]) + downL=numpy.array(continuo[0,o-1]) + downF=numpy.array(continuo[1,o-1]) + larg=laro[o] + lamf=longitud[larg-1] + lami=lamf-5 + + I = numpy.where((downL>=lami) & (downL<=lamf))[0] + if len(I)>4: + veL = downL[I] + veF = downF[I] + + ajuster = optimize.leastsq(res_lin,guess,args=(veF,veL)) + pen = ajuster[0][0] + + j = 0 + while j < 3: + + flujo = numpy.array(continuo[1,o]) + I = numpy.where(longitud<=lami)[0][-1] + x = longitud[I] + y = flujo[I] + + coe = y-pen*x + + flujo[I:] = longitud[I:]*pen+coe + + ajustec = optimize.leastsq(res_cur,guess2,args=(flujo,longitud)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + + tri=A0+A1*(longitud[:]**1)+A2*(longitud[:]**2)+A3*(longitud[:]**3)+A4*(longitud[:]**4) + continuo[1,o]=list(tri) + + j=j+1 + + AJUSTE_FINAL = A0+A1*(L[o]**1)+A2*(L[o]**2)+A3*(L[o]**3)+A4*(L[o]**4) + + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + + else: + AJUSTE_FINAL = coefs[o,0]+coefs[o,1]*(L[o]**1)+coefs[o,2]*(L[o]**2)+coefs[o,3]*(L[o]**3)+coefs[o,4]*(L[o]**4) + NORMALIZADO[o] = list(F[o]/AJUSTE_FINAL) + o=o-1 + + AAAA = numpy.zeros(ordenes,list) + BBBB = numpy.zeros(ordenes,list) + + o = 0 + while o < ordenes: + + AAAA[o] = numpy.array(NORMALIZADO[o]) + BBBB[o]=L[o] + + o = o+1; + + return BBBB,AAAA; def NORM_single(L, F, orden = 5): - warnings.simplefilter('ignore', numpy.RankWarning) - - pixeles = L.shape[0] - guess = [-0.1,1.0] - guess2 = [1,0.1,0.1,0.1,0.1] - - div = 4 - rec = 20 - nnn = orden - para = numpy.zeros( (2,div),float ) - #strli,strlf = numpy.loadtxt('/data/echelle/ecpipe/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) - strli = numpy.array([3965.0,3930.0]) - strlf = numpy.array([3975.0,3940.0]) - unos = numpy.zeros(pixeles,float) + 1.0 - - Lmien = numpy.zeros(pixeles,float) - Fmien = numpy.zeros(pixeles,float) - - """ - elemino flujos nulos - """ - - I = numpy.where( F != 0.0)[0] - Llimpio = L[I] - Flimpio = F[I] - - """ - calculo mediana con 5 pixeles - """ - - i = 0 - k = 0 - - largo_nul = Llimpio.shape[0] - - while i < largo_nul: - if i+9 < largo_nul: - Lmien[k] = numpy.mean(Llimpio[i:i+5]) - Fmien[k] = numpy.median(Flimpio[i:i+5]) - i = i+5 - k = k+1 - else: - Lmien[k] = numpy.mean(Llimpio[i:largo_nul]) - Fmien[k] = numpy.median(Flimpio[i:largo_nul]) - i = i+9 - k = k+1 - - largo_fil = k - Llimpio = Lmien[0:largo_fil] - Flimpio = Fmien[0:largo_fil] - - """ - no considero strong lines: - """ - - rellenado = False - - j = 0 - while j strli[j]) & (Llimpio < strlf[j]) )[0] - Flimpio[I]=-1 - - if I.shape[0]>0: - rellenado == True - - j=j+1 - - """ - completo flujo de strong lines con ajuste lineal - """ - if rellenado: - #print '!!!!!!!!!!!!!!!!!!!' - Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) - #print sys.getrefcount(Flimpio) - - """ - divido cada orden en 3 y ajusto una recta - """ - - can = int(Llimpio.shape[0]/div) - mm = numpy.zeros(div,float) - nn = numpy.zeros(div,float) - - c=0 - while c < div: - - vectorL = Llimpio[c*can:(c+1)*can+1] - vectorF = Flimpio[c*can:(c+1)*can+1] - para = division(vectorL,vectorF,rec) - - mm[c] = para[0][0] - nn[c] = para[0][1] - c = c+1 - - c = 0 - FF = [] - LL = [] - - while c < div: - - m = mm[c] - n = nn[c] - - if c != div-1: - segl = Llimpio[c*can:(c+1)*can] - segf = segl*m+n - elif c == div-1: - segl = Llimpio[c*can:Llimpio.shape[0]] - segf = segl*m+n - - LL = LL+list(segl) - FF = FF+list(segf) - c = c+1 - - LL = numpy.array(LL) - FF = numpy.array(FF) - ajustep = numpy.polyfit(LL,FF,nnn) - FF = numpy.polyval(ajustep,LL) - """ - ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) - a0=ajustep[0][0] - a1=ajustep[0][1] - a2=ajustep[0][2] - a3=ajustep[0][3] - a4=ajustep[0][4] - - FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) - """ - """ - considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 - """ - - JJ = FF.copy() - RES = Flimpio-FF - veva = numpy.zeros(pixeles,float) - AJ = numpy.zeros((Flimpio.shape[0]),float) - - WI = numpy.where(RES < 0.0)[0] - devm = math.sqrt(numpy.mean(RES[WI]*RES[WI])) - - I = numpy.where(RES > 0.0)[0] - veva = RES[I] - - dev = math.sqrt(numpy.mean(veva*veva)) - - i=0 - while i -devm and RES[i] <= 4*dev: - AJ[i] = Flimpio[i] - elif RES[i] > 4*dev: - AJ[i] = -1 - Flimpio[i] = FF[i] - else: - AJ[i] = -1 - - i=i+1 - - JJ = FF+dev - - if AJ[0 ]== -1: - AJ[0] = numpy.median(Flimpio[0:20]) - if AJ[-1] == -1: - AJ[-1] = numpy.median(Flimpio[-21:-1]) - - FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) - - LLL = Llimpio.copy() - ajustep = numpy.polyfit(LLL,FFF,nnn) - CON = numpy.polyval(ajustep,LLL) - """ - ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) - a0 = ajustep[0][0] - a1 = ajustep[0][1] - a2 = ajustep[0][2] - a3 = ajustep[0][3] - a4 = ajustep[0][4] - - CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) - """ - """ - itero para subir el continuo - """ - - #print "iterando para subir el continuo" - - j = 0 - while j < 10: - - - RESI = Flimpio-CON - larg = Flimpio.shape[0] - - loco = Llimpio.copy() - foco = Flimpio.copy() - - I = numpy.where(RESI > 0.0) - CON[I] = foco[I] - - ajustec = numpy.polyfit(loco,CON,nnn) - aji = numpy.polyval(ajustec,loco) - - """ - ajustec = optimize.leastsq(res_cur,guess2,args=(CON,loco)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) - """ - - CON = aji.copy() - - - j=j+1 - - I = numpy.where( (Llimpio > Llimpio[0]+5.0) & (Llimpio < Llimpio[0]+10.0) )[0] - if len(I) > 2: - ajl = numpy.polyfit(Llimpio[I],aji[I],1) - pendi = ajl[0] - """ - ajl = optimize.leastsq(res_lin,guess,args=(aji[I],Llimpio[I])) - pendi = ajl[0][0] - """ - coefi = aji[I[0]]-pendi*Llimpio[I[0]] - aj2 = aji.copy() - aj2[0:I[0]] = Llimpio[0:I[0]]*pendi + coefi - else: - aj2 = aji.copy() - - I = numpy.where( (Llimpio > Llimpio[-1]-10.0) & (Llimpio < Llimpio[-1]-5.0) )[0] - if len(I) > 2: - ajl = numpy.polyfit(Llimpio[I],aji[I],1) - pendi = ajl[0] - """ - ajl = optimize.leastsq(res_lin,guess,args=(aji[I],Llimpio[I])) - pendi = ajl[0][0] - """ - coefi = aji[I[-1]]-pendi*Llimpio[I[-1]] - aj2[I[-1]:] = Llimpio[I[-1]:]*pendi + coefi - else: - aj2 = aji.copy() - ajustec = numpy.polyfit(Llimpio,aj2,nnn) - ajf = numpy.polyval(ajustec,L) - """ - ajustec = optimize.leastsq(res_cur,guess2,args=(aj2,Llimpio)) - A0 = ajustec[0][0] - A1 = ajustec[0][1] - A2 = ajustec[0][2] - A3 = ajustec[0][3] - A4 = ajustec[0][4] - - ajf = A0+A1*(L**1)+A2*(L**2)+A3*(L**3)+A4*(L**4) - """ - NF = F/ajf - - gc.collect() - #print sys.getrefcount(para) - #print sys.getrefcount(unos) - #print sys.getrefcount(Lmien) - return ajustec \ No newline at end of file + warnings.simplefilter('ignore', numpy.RankWarning) + + pixeles = L.shape[0] + guess = [-0.1,1.0] + guess2 = [1,0.1,0.1,0.1,0.1] + + div = 4 + rec = 20 + nnn = orden + para = numpy.zeros( (2,div),float ) + #strli,strlf = numpy.loadtxt('/data/echelle/ecpipe/Continuum/strong_lines.dat',dtype=None,usecols=(0,1),unpack=True) + strli = numpy.array([3965.0,3930.0]) + strlf = numpy.array([3975.0,3940.0]) + unos = numpy.zeros(pixeles,float) + 1.0 + + Lmien = numpy.zeros(pixeles,float) + Fmien = numpy.zeros(pixeles,float) + + """ + elemino flujos nulos + """ + + I = numpy.where( F != 0.0)[0] + Llimpio = L[I] + Flimpio = F[I] + + """ + calculo mediana con 5 pixeles + """ + + i = 0 + k = 0 + + largo_nul = Llimpio.shape[0] + + while i < largo_nul: + if i+9 < largo_nul: + Lmien[k] = numpy.mean(Llimpio[i:i+5]) + Fmien[k] = numpy.median(Flimpio[i:i+5]) + i = i+5 + k = k+1 + else: + Lmien[k] = numpy.mean(Llimpio[i:largo_nul]) + Fmien[k] = numpy.median(Flimpio[i:largo_nul]) + i = i+9 + k = k+1 + + largo_fil = k + Llimpio = Lmien[0:largo_fil] + Flimpio = Fmien[0:largo_fil] + + """ + no considero strong lines: + """ + + rellenado = False + + j = 0 + while j strli[j]) & (Llimpio < strlf[j]) )[0] + Flimpio[I]=-1 + + if I.shape[0]>0: + rellenado == True + + j=j+1 + + """ + completo flujo de strong lines con ajuste lineal + """ + if rellenado: + #print '!!!!!!!!!!!!!!!!!!!' + Flimpio = FunNorm.Rell(Llimpio.astype("double"),Flimpio.astype("double"),Flimpio.shape[0]) + #print sys.getrefcount(Flimpio) + + """ + divido cada orden en 3 y ajusto una recta + """ + + can = int(Llimpio.shape[0]/div) + mm = numpy.zeros(div,float) + nn = numpy.zeros(div,float) + + c=0 + while c < div: + + vectorL = Llimpio[c*can:(c+1)*can+1] + vectorF = Flimpio[c*can:(c+1)*can+1] + para = division(vectorL,vectorF,rec) + + mm[c] = para[0][0] + nn[c] = para[0][1] + c = c+1 + + c = 0 + FF = [] + LL = [] + + while c < div: + + m = mm[c] + n = nn[c] + + if c != div-1: + segl = Llimpio[c*can:(c+1)*can] + segf = segl*m+n + elif c == div-1: + segl = Llimpio[c*can:Llimpio.shape[0]] + segf = segl*m+n + + LL = LL+list(segl) + FF = FF+list(segf) + c = c+1 + + LL = numpy.array(LL) + FF = numpy.array(FF) + ajustep = numpy.polyfit(LL,FF,nnn) + FF = numpy.polyval(ajustep,LL) + """ + ajustep=optimize.leastsq(res_cur,guess2,args=(FF,LL)) + a0=ajustep[0][0] + a1=ajustep[0][1] + a2=ajustep[0][2] + a3=ajustep[0][3] + a4=ajustep[0][4] + + FF=a0+a1*LL+a2*(LL**2)+a3*(LL**3)+a4*(LL**4) + """ + """ + considero los peaks sobre 1 sigma y ajusto polinomio de orden 4 + """ + + JJ = FF.copy() + RES = Flimpio-FF + veva = numpy.zeros(pixeles,float) + AJ = numpy.zeros((Flimpio.shape[0]),float) + + WI = numpy.where(RES < 0.0)[0] + devm = math.sqrt(numpy.mean(RES[WI]*RES[WI])) + + I = numpy.where(RES > 0.0)[0] + veva = RES[I] + + dev = math.sqrt(numpy.mean(veva*veva)) + + i=0 + while i -devm and RES[i] <= 4*dev: + AJ[i] = Flimpio[i] + elif RES[i] > 4*dev: + AJ[i] = -1 + Flimpio[i] = FF[i] + else: + AJ[i] = -1 + + i=i+1 + + JJ = FF+dev + + if AJ[0 ]== -1: + AJ[0] = numpy.median(Flimpio[0:20]) + if AJ[-1] == -1: + AJ[-1] = numpy.median(Flimpio[-21:-1]) + + FFF = FunNorm.Rell(Llimpio,AJ,Flimpio.shape[0]) + + LLL = Llimpio.copy() + ajustep = numpy.polyfit(LLL,FFF,nnn) + CON = numpy.polyval(ajustep,LLL) + """ + ajustep = optimize.leastsq(res_cur,guess2,args=(FFF,LLL)) + a0 = ajustep[0][0] + a1 = ajustep[0][1] + a2 = ajustep[0][2] + a3 = ajustep[0][3] + a4 = ajustep[0][4] + + CON = a0+a1*LLL+a2*(LLL**2)+a3*(LLL**3)+a4*(LLL**4) + """ + """ + itero para subir el continuo + """ + + #print "iterando para subir el continuo" + + j = 0 + while j < 10: + + + RESI = Flimpio-CON + larg = Flimpio.shape[0] + + loco = Llimpio.copy() + foco = Flimpio.copy() + + I = numpy.where(RESI > 0.0) + CON[I] = foco[I] + + ajustec = numpy.polyfit(loco,CON,nnn) + aji = numpy.polyval(ajustec,loco) + + """ + ajustec = optimize.leastsq(res_cur,guess2,args=(CON,loco)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + aji = A0+A1*(loco[0:larg]**1)+A2*(loco[0:larg]**2)+A3*(loco[0:larg]**3)+A4*(loco[0:larg]**4) + """ + + CON = aji.copy() + + + j=j+1 + + I = numpy.where( (Llimpio > Llimpio[0]+5.0) & (Llimpio < Llimpio[0]+10.0) )[0] + if len(I) > 2: + ajl = numpy.polyfit(Llimpio[I],aji[I],1) + pendi = ajl[0] + """ + ajl = optimize.leastsq(res_lin,guess,args=(aji[I],Llimpio[I])) + pendi = ajl[0][0] + """ + coefi = aji[I[0]]-pendi*Llimpio[I[0]] + aj2 = aji.copy() + aj2[0:I[0]] = Llimpio[0:I[0]]*pendi + coefi + else: + aj2 = aji.copy() + + I = numpy.where( (Llimpio > Llimpio[-1]-10.0) & (Llimpio < Llimpio[-1]-5.0) )[0] + if len(I) > 2: + ajl = numpy.polyfit(Llimpio[I],aji[I],1) + pendi = ajl[0] + """ + ajl = optimize.leastsq(res_lin,guess,args=(aji[I],Llimpio[I])) + pendi = ajl[0][0] + """ + coefi = aji[I[-1]]-pendi*Llimpio[I[-1]] + aj2[I[-1]:] = Llimpio[I[-1]:]*pendi + coefi + else: + aj2 = aji.copy() + ajustec = numpy.polyfit(Llimpio,aj2,nnn) + ajf = numpy.polyval(ajustec,L) + """ + ajustec = optimize.leastsq(res_cur,guess2,args=(aj2,Llimpio)) + A0 = ajustec[0][0] + A1 = ajustec[0][1] + A2 = ajustec[0][2] + A3 = ajustec[0][3] + A4 = ajustec[0][4] + + ajf = A0+A1*(L**1)+A2*(L**2)+A3*(L**3)+A4*(L**4) + """ + NF = F/ajf + + gc.collect() + #print sys.getrefcount(para) + #print sys.getrefcount(unos) + #print sys.getrefcount(Lmien) + return ajustec diff --git a/utils/Continuum/oldFN.c b/utils/Continuum/oldFN.c index 978ec18..354986c 100644 --- a/utils/Continuum/oldFN.c +++ b/utils/Continuum/oldFN.c @@ -5,100 +5,100 @@ static PyObject *FunNorm_Rell(PyObject *self,PyObject *args){ -int i,j,n_datos,u=1,fin; -double pref,prel,postl,postf,m,n; + int i,j,n_datos,u=1,fin; + double pref,prel,postl,postf,m,n; -double *CLam; -PyObject *PyLam; + double *CLam; + PyObject *PyLam; -double *CFlux; -PyObject *PyFlux; + double *CFlux; + PyObject *PyFlux; -PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); -CLam = ARRAYD(PyLam); -CFlux = ARRAYD(PyFlux); + PyArg_ParseTuple(args,"OOi",&PyLam,&PyFlux,&n_datos); + CLam = ARRAYD(PyLam); + CFlux = ARRAYD(PyFlux); -PyObject *myList = PyList_New(n_datos); + PyObject *myList = PyList_New(n_datos); -if(CFlux[n_datos-1]==-1){ - j=n_datos-2; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[n_datos-1]=CFlux[j]; - break; - } - j=j-1; + if(CFlux[n_datos-1]==-1){ + j=n_datos-2; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[n_datos-1]=CFlux[j]; + break; + } + j=j-1; - } - } + } + } -if(CFlux[0]==-1){ - j=0; - while(j>=0){ - if(CFlux[j]!=-1){ - CFlux[0]=CFlux[j]; - break; - } - j++; + if(CFlux[0]==-1){ + j=0; + while(j>=0){ + if(CFlux[j]!=-1){ + CFlux[0]=CFlux[j]; + break; + } + j++; - } - } + } + } -i=0; -while(i0){ - pref=CFlux[i-1]; - prel=CLam[i-1]; + i=0; + while(i0){ + pref=CFlux[i-1]; + prel=CLam[i-1]; - } - if(CFlux[i+1]>0){ - postf=CFlux[i+1]; - postl=CLam[i+1]; - m=(postf-pref)/(postl-prel); - n=postf-m*postl; - u=0; - fin=i+1; - } - i=i+1; - - } - else{ - u=0; - } - } - i=j; - if(CFlux[i]<0){ - while(i0){ + postf=CFlux[i+1]; + postl=CLam[i+1]; + m=(postf-pref)/(postl-prel); + n=postf-m*postl; + u=0; + fin=i+1; } - i=j; - PyObject *num = PyFloat_FromDouble(CFlux[i]); - if(!num){ - Py_DECREF(myList); - return NULL; - } - PyList_SET_ITEM(myList,i,num); + i=i+1; + + } + else{ + u=0; + } + + } + i=j; + if(CFlux[i]<0){ + while(i= 3 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "FunNorm", + NULL, + 0, //no state, so re-initialization is fine + FunNormMethods, + NULL, + NULL, + NULL, + NULL +}; +PyMODINIT_FUNC PyInit_FunNorm(void) +{ + PyObject* module = PyModule_Create(&moduledef); + return module; +} + +#else +void initFunNorm(void){ + (void) Py_InitModule("FunNorm", FunNormMethods); +} +#endif diff --git a/utils/Continuum/setup.py b/utils/Continuum/setup.py index 193b978..48f67bf 100644 --- a/utils/Continuum/setup.py +++ b/utils/Continuum/setup.py @@ -1,3 +1,4 @@ +from __future__ import print_function from distutils.core import setup, Extension import numpy diff --git a/utils/Correlation/all.py b/utils/Correlation/all.py index 4f057a9..22fa2b5 100644 --- a/utils/Correlation/all.py +++ b/utils/Correlation/all.py @@ -1,3 +1,4 @@ +from __future__ import print_function import pyfits import numpy import os @@ -14,22 +15,21 @@ nam = [] mods = mods[:-5] for fits in mods: - print fits - sc = pyfits.getdata(dire+fits) - L,F = sc[0],sc[1] - Ln,Fn = continuum.NORM2(L,F) - Tfin, Gfin, Zfin, rot, velo2 = correlation.CCF(Ln,Fn) - t.append(Tfin) - g.append(Gfin) - z.append(Zfin) - r.append(rot) - v.append(velo2) - nam.append(fits) + print(fits) + sc = pyfits.getdata(dire+fits) + L,F = sc[0],sc[1] + Ln,Fn = continuum.NORM2(L,F) + Tfin, Gfin, Zfin, rot, velo2 = correlation.CCF(Ln,Fn) + t.append(Tfin) + g.append(Gfin) + z.append(Zfin) + r.append(rot) + v.append(velo2) + nam.append(fits) cant = len(t) o=0 while o< cant: - print nam[o],t[o],g[o],z[o],r[o],v[o] - o+=1 - + print(nam[o],t[o],g[o],z[o],r[o],v[o]) + o+=1 diff --git a/utils/Correlation/anchos.py b/utils/Correlation/anchos.py index ad9f14f..c64bd1b 100644 --- a/utils/Correlation/anchos.py +++ b/utils/Correlation/anchos.py @@ -1,3 +1,4 @@ +from __future__ import print_function import pyfits import numpy import scipy @@ -9,23 +10,23 @@ import matplotlib.pylab as plt import readcol def gauss1(params,x): - C = params[0] - A = params[1] - med = params[2] - sig = params[3] - g = C+A*numpy.exp(-0.5*(x-med)*(x-med)/(sig*sig)) - return g + C = params[0] + A = params[1] + med = params[2] + sig = params[3] + g = C+A*numpy.exp(-0.5*(x-med)*(x-med)/(sig*sig)) + return g def res_gauss1(params,g,x): - return g-gauss1(params,x) + return g-gauss1(params,x) def el_stl(wam,fl,SLi,SLf): - for i in range(len(SLi)): - if SLi[i]>wam[-1]: - break - I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] - fl[I]=1.0 - return fl + for i in range(len(SLi)): + if SLi[i]>wam[-1]: + break + I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] + fl[I]=1.0 + return fl SLi,SLf = readcol.readcol('lines2.dat',twod=False) model_path = '/media/VERBATIM/COHELO_MODELS/RES_MOD/R_60000b/' @@ -38,48 +39,47 @@ def el_stl(wam,fl,SLi,SLf): verdad = False non_rot = non_rot[200:] for fits in non_rot: - - sc = pyfits.getdata(model_path+'vsini_0.0/'+fits) - hd = pyfits.getheader(model_path+'vsini_0.0/'+fits) - wa1 = numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1'] - sc = el_stl(wa1,sc,SLi,SLf) - wa = toVac.ToVacuum(wa1) - I = numpy.where((wa > 5500) & (wa < 6250))[0] - wa = wa[I] - fl = sc[I] - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - devs = '' - if T == 4500 and G == 2.5 and Z == -1.5: - verdad=True - #elif T == 4000 and G == 3.0: - # break - if verdad: - for v in rot: - if os.access(model_path+'vsini_'+str(v)+'/'+'R_'+str(v)+'_'+fits[-22:],os.F_OK): - sc2 = pyfits.getdata(model_path+'vsini_'+str(v)+'/'+'R_'+str(v)+'_'+fits[-22:]) - sc2 = el_stl(wa1,sc2,SLi,SLf) - fl2 = sc2[I] - vv,cc = prueba.CCF(wa,fl,wa,fl2,-200.0,200.0) - cc2 = numpy.array(cc) - vv2 = numpy.array(vv) - B3 = 0.5*(cc2[0]+cc2[-1]) - A3 = numpy.max(cc2)-B3 - med3 = 0.0 - sig3 = 20.0 - guess1 = [B3,A3,med3,sig3] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) - cte3 = ajustep[0][0] - no3 = ajustep[0][1] - med3 = ajustep[0][2] - sig3 = ajustep[0][3] - - devs = devs+str(sig3)+'\t' - - else: - devs = devs+str(0.0)+'\t' - f.write(str(T)+'\t'+str(G)+'\t'+str(Z)+'\t'+devs+'\n') - - print T,G,Z,devs + sc = pyfits.getdata(model_path+'vsini_0.0/'+fits) + hd = pyfits.getheader(model_path+'vsini_0.0/'+fits) + wa1 = numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1'] + sc = el_stl(wa1,sc,SLi,SLf) + wa = toVac.ToVacuum(wa1) + I = numpy.where((wa > 5500) & (wa < 6250))[0] + wa = wa[I] + fl = sc[I] + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + devs = '' + if T == 4500 and G == 2.5 and Z == -1.5: + verdad=True + #elif T == 4000 and G == 3.0: + # break + if verdad: + for v in rot: + if os.access(model_path+'vsini_'+str(v)+'/'+'R_'+str(v)+'_'+fits[-22:],os.F_OK): + sc2 = pyfits.getdata(model_path+'vsini_'+str(v)+'/'+'R_'+str(v)+'_'+fits[-22:]) + sc2 = el_stl(wa1,sc2,SLi,SLf) + fl2 = sc2[I] + vv,cc = prueba.CCF(wa,fl,wa,fl2,-200.0,200.0) + cc2 = numpy.array(cc) + vv2 = numpy.array(vv) + B3 = 0.5*(cc2[0]+cc2[-1]) + A3 = numpy.max(cc2)-B3 + med3 = 0.0 + sig3 = 20.0 + guess1 = [B3,A3,med3,sig3] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) + cte3 = ajustep[0][0] + no3 = ajustep[0][1] + med3 = ajustep[0][2] + sig3 = ajustep[0][3] + + devs = devs+str(sig3)+'\t' + + else: + devs = devs+str(0.0)+'\t' + f.write(str(T)+'\t'+str(G)+'\t'+str(Z)+'\t'+devs+'\n') + + print(T,G,Z,devs) diff --git a/utils/Correlation/correlation.py b/utils/Correlation/correlation.py index 5046831..2308853 100644 --- a/utils/Correlation/correlation.py +++ b/utils/Correlation/correlation.py @@ -1,3 +1,4 @@ +from __future__ import print_function #!/usr/bin/python from pylab import * import sys @@ -15,8 +16,8 @@ from multiprocessing import Pool def n_Edlen(l): - """ - Refractive index according to Edlen 1966 + """ + Refractive index according to Edlen 1966 """ @@ -37,7 +38,7 @@ def n_Morton(l): return n def ToAir(l): - """ + """ Transforms input wavelength (A) from Vacuum to Air Uses refractive index as per Edlen 1966 @@ -47,7 +48,7 @@ def ToAir(l): return (l / n_Edlen(l)) def ToVacuum(l): - """ + """ Transforms input wavelength (A) from Air to Vacuum Uses refractive index as per Edlen 1966 @@ -61,1121 +62,1121 @@ def ToVacuum(l): l_new = n_Edlen(l_prev) * l if (max(abs(l_new - l_prev)) < 1e-10): cond = 0 l_prev = l_new - + return l_prev def el_stl(wam,fl,SLi,SLf): - for i in range(len(SLi)): - if SLi[i]>wam[-1]: - break - I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] - fl[I]=1.0 - return fl + for i in range(len(SLi)): + if SLi[i]>wam[-1]: + break + I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] + fl[I]=1.0 + return fl def corr_p(model_name): - sc = pyfits.getdata(model_name) - FF = sc[It] - mwa = wat*(1+velo2/lux) - - CCF,NOR = 0,0 - - o = 0 - while o < L_Gl.shape[0]: - #print L[o] - k = 0 - while k= L_Gl.shape[0]: - break - I = numpy.where(F_Gl[o]!=0.0)[0] - if I.shape[0] > 0: - Fl = F_Gl[o][I] - Ll = L_Gl[o][I] - - I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] - MML = mwa[I] - MF = FF[I] - #ajustec = continuum.NORM_single(MML, MF, orden=2) - #plot( MML, MF ) - #plot( MML, numpy.polyval(ajustec, MML)) - - #NMF = MF / numpy.polyval(ajustec, MML) - tckM = interpolate.splrep(MML,MF,k=3,s=0) - NFM = interpolate.splev(Ll,tckM,der=0) - - a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) - b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) - c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) - if c < 0: - plot(MML,MF) - plot(Ll,Fl) - plot(Ll,NFM) - show() - CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) - NOR = NOR+Ll[-2]-Ll[1] - - o += 1 - #show() - return CCF/NOR + sc = pyfits.getdata(model_name) + FF = sc[It] + mwa = wat*(1+velo2/lux) + + CCF,NOR = 0,0 + + o = 0 + while o < L_Gl.shape[0]: + #print L[o] + k = 0 + while k= L_Gl.shape[0]: + break + I = numpy.where(F_Gl[o]!=0.0)[0] + if I.shape[0] > 0: + Fl = F_Gl[o][I] + Ll = L_Gl[o][I] + + I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] + MML = mwa[I] + MF = FF[I] + #ajustec = continuum.NORM_single(MML, MF, orden=2) + #plot( MML, MF ) + #plot( MML, numpy.polyval(ajustec, MML)) + + #NMF = MF / numpy.polyval(ajustec, MML) + tckM = interpolate.splrep(MML,MF,k=3,s=0) + NFM = interpolate.splev(Ll,tckM,der=0) + + a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) + b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) + c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) + if c < 0: + plot(MML,MF) + plot(Ll,Fl) + plot(Ll,NFM) + show() + CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) + NOR = NOR+Ll[-2]-Ll[1] + + o += 1 + #show() + return CCF/NOR def corr(L,F,mwa,FF,ons): - CCF,NOR = 0,0 - - o = 0 - while o < L.shape[0]: - #print L[o] - k = 0 - while k= L.shape[0]: - break - - I = numpy.where(F[o]!=0.0)[0] - if I.shape[0] > 0: - Fl = F[o][I] - Ll = L[o][I] - - I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] - MML = mwa[I] - MF = FF[I] - #ajustec = continuum.NORM_single(MML, MF, orden=2) - #plot( MML, MF ) - #plot( MML, numpy.polyval(ajustec, MML)) - - #NMF = MF / numpy.polyval(ajustec, MML) - tckM = interpolate.splrep(MML,MF,k=3,s=0) - NFM = interpolate.splev(Ll,tckM,der=0) - - a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) - b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) - c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) - if c < 0: - plot(MML,MF) - plot(Ll,Fl) - plot(Ll,NFM) - show() - CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) - NOR = NOR+Ll[-2]-Ll[1] - - o += 1 - #show() - return CCF/NOR + CCF,NOR = 0,0 + + o = 0 + while o < L.shape[0]: + #print L[o] + k = 0 + while k= L.shape[0]: + break + + I = numpy.where(F[o]!=0.0)[0] + if I.shape[0] > 0: + Fl = F[o][I] + Ll = L[o][I] + + I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] + MML = mwa[I] + MF = FF[I] + #ajustec = continuum.NORM_single(MML, MF, orden=2) + #plot( MML, MF ) + #plot( MML, numpy.polyval(ajustec, MML)) + + #NMF = MF / numpy.polyval(ajustec, MML) + tckM = interpolate.splrep(MML,MF,k=3,s=0) + NFM = interpolate.splev(Ll,tckM,der=0) + + a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) + b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) + c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) + if c < 0: + plot(MML,MF) + plot(Ll,Fl) + plot(Ll,NFM) + show() + CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) + NOR = NOR+Ll[-2]-Ll[1] + + o += 1 + #show() + return CCF/NOR def bad_orders(L,bl): - bo = [] - - o = 0 - while o < L.shape[0]: - i=0 - while ibl[i][0]) & (L[o]bl[i][0]) & (L[o] 0: - or01 = i - break - - or02 = L1.shape[0]-1 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < Li)[0] - if len(I) > 0: - or02 = i - break - - or03 = 0 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 6250.0)[0] - if len(I) > 0: - or03 = i - break - - or04 = L1.shape[0]-1 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 5500.0)[0] - if len(I) > 0: - or04 = i - break - - or05 = 0 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 5190.0)[0] - W = numpy.where(L1[i] < 5170.0)[0] - if len(I) > 0 and len(W) > 0: - or05 = i - break - #print or01,or02,or03,or04,or05 - guess = [1.0,1.0,1.0] - - L = L1[or01:or02] - F = F1[or01:or02] - Lm = L1[or03:or04] - Fm = F1[or03:or04] - Lg = L1[or05] - Fg = F1[or05] - - - bad_lines = [[6860,6900],[6550,6580],[6270,6320],[4850,4880]] - ons = bad_orders(L,bad_lines) - - modi = 'R_0.0_5000_30_p00p00.ms.fits' - #print 'Radial velocity calculation via CCF with: '+modi - - sci = pyfits.getdata(model_path+'vsini_0.0/'+modi) - hdi = pyfits.getheader(model_path+'vsini_0.0/'+modi) - - wam1 = ToVacuum(numpy.arange(len(sci))*hdi['CD1_1']+hdi['CRVAL1']) - - Im = numpy.where((wam1 > 5400.0) & (wam1 < 6350.0))[0] - wam = wam1[Im] - flm = sci[Im] - - Ig = numpy.where((wam1 > 5100.0) & (wam1 < 5250.0))[0] - wag = wam1[Ig] - - It = numpy.where((wam1 > 4000.0) & (wam1 < 7500.0))[0] - wat = wam1[It] - - - for i in range(Lm.shape[0]): - I = numpy.where((Fm[i] != 0.0) & (Fm[i] < 2.0))[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I] - MFs = flm[I] - - vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200.0,200.0) - - if i == 0: - ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = Ls[-1]-Ls[0] - else: - ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - - ccf = ccf/nor - vv = numpy.array(vv) - - B = 0.5*(ccf[0]+ccf[-1]) - A = numpy.max(ccf)-B - med = vv[numpy.where(ccf == numpy.max(ccf))[0]] - sig = 20.0 - guess1 = [B,A,med,sig] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) - velo = ajustep[0][2] - - #print 'The initial radial velocity is: '+str(velo)+' km/s' - - #print 'Determining parameters of the initial model' - - vecti = [3500,4000,4500,5000,5500,6500] - vecgi = [0.0,1.5, 3.0,4.5] - #veczi = [-2.0,-1.0,0.0] - veczi = [-1.0,0.0] - ccmax = 0 - names = [] - cc = 0 - nor = 0 - for g in vecgi: - for z in veczi: - for t in vecti: - nam = get_name(t,g,z) - names.append(nam) - namemax = get_name(5500,4.5,0.0) - TEI = 5500 - MEI = 0.0 - LGI = 4.5 - for nam in names: - if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): - mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) - flm = mod[Im] - cc = 0.0 - nor = 0.0 - for o in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I]*(1+velo/lux) - MFs = flm[I] - - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - cc = cc + integrate.simps(NMFs[1:-1]*Fs[1:-1],Ls[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]))*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - cc = cc/nor - - if cc >=ccmax: - ccmax = cc - namemax = nam - - mod = pyfits.getheader(model_path+'vsini_0.0/R_0.0_'+namemax) - TEI = mod['TEFF'] - - #print 'Teff (initial) = '+str(TEI)+' K' - - - - if TEI <= 4000: - rot = 5.0 - LGI = 3.0 - MTI = 0.0 - late = True - velo2 = velo - - else: - late = False - t = TEI - vecgi = [1.0,2.0,3.0,4.0] - #veczi = [-2.0,-1.0,0.0] - veczi = [-1.0,0.0] - dif = 1000.0 - - for z in veczi: - vals = [] - di = 1000 - meg = 3.0 - for g in vecgi: - nam = get_name(t,g,z) - if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): - mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) - flm = mod[Im] - flm = el_stl(wam,flm,SLi,SLf) - intfl = 0.0 - intflm = 0.0 - di = 1000 - for o in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I]*(1+velo/lux) - MFs = flm[I] - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - intfl = intfl + integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1]) - intflm = intflm + integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]) - intfl = math.sqrt(intfl) - intflm = math.sqrt(intflm) - dif1 = math.sqrt((intflm-intfl)**2) - vals.append(dif1) - if dif1 < di: - di = dif1 - meg = g - dif2 = numpy.mean(vals) - #print z,dif2 - if dif2 Ls[0]-5.0) & (wag < Ls[-1]+5.0))[0] - MLs = wag[I]*(1+velo/lux) - MFs = mflg[I] - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - cc2 = integrate.simps(Fs[1:-1]*NMFs[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1])) - #print g,cc2 - if cc2 > cfi2: - cfi2 = cc2 - LGI = g - - #print 'Log(g) (initial) = '+str(LGI) - - itera = 0 - maximo = 0 - calculated = [] - rotss = [] - vecR = [] - vecT = [] - vecG = [] - - vecZ = [] - vecCF = [] - while itera < 4: - - if late==False: - - MOG = get_name(TEI,LGI,MEI) - sc = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+MOG) - - #print '-Calculating radial shift and v*sin(i) with model: '+MOG - - flm = sc[Im] - ies = [] - flm = el_stl(wam,flm,SLi,SLf) - for i in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - ies.append(I) - MLs = wam[I] - MFs = flm[I] - vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200,200) - if i == 0: - ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = Ls[-1]-Ls[0] - else: - ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - nor2=1. - ccf = ccf/nor - vv = numpy.array(vv) - B = 0.5*(ccf[0]+ccf[-1]) - A = numpy.max(ccf)-B - med = vv[numpy.where(ccf == numpy.max(ccf))[0]] - sig = 20.0 - guess1 = [B,A,med,sig] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) - velo2 = ajustep[0][2] - sig2 = ajustep[0][3] - sig2 = math.sqrt(sig2*sig2) - #plt.plot(vv,ccf,vv,gauss1(ajustep[0],vv)) - #plt.show() - #print 'radial velocity = '+str(velo2)+' km/s' - #print 'Sigma = '+str(sig2)+' km/s' - """ - - vi = 0.0 - difsigmin = 1000 - while vi <= 20.0: - ai = 0 - if ( (os.access(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG,os.F_OK) == True)): - modt = pyfits.getdata(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG) - flm2 = modt[Im] - flm2 = el_stl(wam,flm2,SLi,SLf) - for I in ies: - Fs = flm[I] - Ls = wam[I] - Fs = el_stl(Ls,Fs,SLi,SLf) - MFs = flm2[I] - vv2,cc2 = vels.CCF(Ls,MFs,Ls,Fs,-200,200) - if ai == 0: - ccf2 = numpy.array(cc2)*(Ls[-1]-Ls[0]) - nor2 = Ls[-1]-Ls[0] - else: - ccf2 = ccf2 + numpy.array(cc2)*(Ls[-1]-Ls[0]) - nor2 = nor2 + (Ls[-1]-Ls[0]) - ai += 1 - cc2 = ccf2/nor2 - vv2 = numpy.array(vv2) - B3 = 0.5*(cc2[0]+cc2[-1]) - A3 = numpy.max(cc2)-B3 - med3 = 0.0 - sig3 = 20.0 - guess1 = [B3,A3,med3,sig3] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) - cte3 = ajustep[0][0] - no3 = ajustep[0][1] - med3 = ajustep[0][2] - sig3 = ajustep[0][3] - - #plt.plot(vv2,cc2) - - print vi,sig3 - difsig = math.sqrt((sig2-sig3)**2) - if difsig < difsigmin: - difsigmin = difsig - rot = vi - ai +=1 - - if vi <= 7.5: - vi = vi+2.5 - elif vi < 50.0: - vi = vi+5.0 - else: - break - - #plt.show() - """ - if sig2 >= vsini[0]: - I = numpy.where((AT == TEI) & (AG == LGI) & (AZ == MEI))[0][0] - anchos = numpy.array([A000[I],A025[I],A050[I],A075[I],A100[I],A150[I],A200[I],A250[I],A300[I],A350[I],A400[I],A450[I],A500[I]]) - kis = 0 - while kis < len(anchos)-1: - if anchos[kis]>anchos[kis+1]: - portemp = anchos[kis] - anchos[kis] = anchos[kis+1] - anchos[kis+1] = portemp - kis+=1 - tck = interpolate.splrep(anchos,numpy.array(vsini),k=3,s=0) - calrot = interpolate.splev(sig2,tck,der=0) - difs = (numpy.array(vsini) - calrot)**2 - AI = int(numpy.where(difs == numpy.min(difs))[0]) - rot = vsini[AI] - else: - rot = vsini[0] - calrot = 0.0 - - #rot = 2.5 - #print 'v*sin(i) = '+str(calrot)+' km/s' - - RI = numpy.where(numpy.array(rotss) == rot)[0] - - if len(RI) > 0: - break - - else: - - model_path1 = model_path+"vsini_"+str(rot)+"/" - - """ conociendo la velocidad radial realizo una busqueda gruesa de parametros estelares calculando el maximo de la CCF""" - #print "-Searching the optimal stellar model" - - #maxT,maxG,maxZ,maxCor,maxvs = 5500,4.0,0.0,0.0,0.0 - maxCor = 0.0 - - if TEI >= 3500 and TEI <=4250: - modT = [3500,4000,4500,5000] - else: - modT = [4000,4500,5000,5500,6000,6500,7000] - - modG = [1.0,2.5,4.0] - #modZ = [-2.0,-1.0,0.0] - modZ = [-1.0,0.0] - MOD,MOD2 = [],[] - - i=0 - while i < len(modZ): - ig = 0 - while ig < len(modG): - it = 0 - while it < len(modT): - MK = 'R_'+str(rot)+'_'+get_name(modT[it],modG[ig],modZ[i]) - if os.access(model_path1+MK,os.F_OK): - MOD.append(MK) - MOD2.append(model_path1+MK) - it = it+1 - ig =ig+1 - i = i+1 - - MOD = np.array(MOD) - MOD2 = np.array(MOD2) - - all_pars = np.vstack((MOD,np.zeros(len(MOD))+velo2)) - L_Gl,F_Gl =L.copy(),F.copy() - p = Pool(npools) - xc_values = np.array((p.map(corr_p, MOD2))) - p.terminate() - I_min = np.argmax(xc_values) - hd = pyfits.getheader(MOD2[I_min]) - maxCor = xc_values[I_min] - maxG = hd['LOG_G'] - maxT = hd['TEFF'] - maxZ = hd['FEH'] - maxvs = hd['VSINI'] - #print MOD2[I_min] - """ - print xc_values - print MOD2[I_min],I_min - print dfgh - - for m in MOD: - - hd = pyfits.getheader(model_path1+m) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - vs = hd['VSINI'] - - sc = pyfits.getdata(model_path1+m) - FF = sc[It] - mwa = wat*(1+velo2/lux) - NCCF = corr(L,F,mwa,FF,ons) - - #print T,G,Z,vs,NCCF - - if NCCF > maxCor: - maxCor = NCCF - maxG = G - maxT = T - maxZ = Z - maxvs = vs - elif NCCF<0: - print "Problem with spectrum!!! -> Negative value of CCF." - maxG = 4.5 - maxT = 5500 - maxZ = 0 - maxvs = 0 - """ - - - - #print 'maxgrueso',maxT,maxG,maxZ,maxvs - - - """ A partir de los parametros encontrados en la busqueda gruesa procedo a constrenir los limites de la busqueda fina""" - - - #Tfin,Gfin,Zfin,rotfin,velfin = 5500,4.0,0.0,0.0,0.0 - """ Ahora se buscan los par'ametros estelares optimos mediante una exploracion fina""" - - if maxT == 3500 or maxT == 4000: - modT = [3500,3750,4000,4250,4500] - - elif maxT == 7000 or maxT == 6500: - modT = [6000,6250,6500,6750,7000] - else: - modT = [maxT-750,maxT-500,maxT-250,maxT,maxT+250,maxT+500,maxT+750] - - if maxG == 1.0: - modG = [0.0,0.5,1.0,1.5,2.0,2.5] - if maxG == 2.5: - modG = [1.0,1.5,2.0,2.5,3.0,3.5,4.0] - if maxG == 4.0: - modG = [2.5,3.0,3.5,4.0,4.5,5.0] - - if maxZ == -2.0: - modZ = [-2.5,-2.0,-1.5,-1.0] - if maxZ == -1.0: - modZ = [-1.5,-1.0,-0.5,0.0] - if maxZ == 0.0: - modZ = [-0.5,0.0,0.2,0.5] - - MOD,MOD2 = [],[] - for i in modZ: - for ig in modG: - for it in modT: - MK = 'R_'+str(rot)+'_'+get_name(it,ig,i) - if os.access(model_path1+MK,os.F_OK): - MOD.append(MK) - MOD2.append(model_path1+MK) - MOD2 = np.array(MOD2) - - L_Gl,F_Gl =L.copy(),F.copy() - p = Pool(npools) - xc_values = np.array((p.map(corr_p, MOD2))) - p.terminate() - I_min = np.argmax(xc_values) - hd = pyfits.getheader(MOD2[I_min]) - maxCor = xc_values[I_min] - maxG = hd['LOG_G'] - maxT = hd['TEFF'] - maxZ = hd['FEH'] - maxvs = hd['VSINI'] - #print MOD2[I_min] - """ - #maxT,maxG,maxZ,maxCor = 5500,4.0,0,0 - maxCor = 0.0 - for m in MOD: - calculated.append(m) - hd = pyfits.getheader(model_path1+m) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path1+m) - FF = sc[It] - mwa = wat*(1+velo2/lux) - - NCCF = corr(L,F,mwa,FF,ons) - - vecT.append(T) - vecG.append(G) - vecZ.append(Z) - vecR.append(rot) - vecCF.append(NCCF) - - #print T,G,Z,NCCF - - if NCCF > maxCor: - maxCor = NCCF - maxT = T - maxG = G - maxZ = Z - - elif NCCF < 0: - print "Problem with spectrum!!! -> Negative value of CCF." - maxG = 4.5 - maxT = 5500 - maxZ = 0 - maxvs = 0 - """ - #print "maxfino",maxT,maxG,maxZ,maxCor - - TEI = maxT - LGI = maxG - MEI = maxZ - - ultrot = rot - - if maxCor > maximo: - maximo = maxCor - Tfin,Gfin,Zfin,rotfin,velfin = maxT,maxG,maxZ,rot,velo2 - #Tf,Gf,Zf = intert,interg,maZ - - late = False - rotss.append(rot) - - - itera = itera+1 - if maximo == 0: - Tfin,Gfin,Zfin,rotfin,velfin = 5500, 4.5,0,0,velo2 - #print 'Pars fase 0:', Tfin, Gfin, Zfin,rotfin,maximo - mejor = False - if rotfin == 0.0: - nrot = [0.0,2.5] - elif rotfin == 2.5: - nrot = [0.0,2.5,5.0] - elif rotfin == 5.0: - nrot = [2.5,5.0,7.5] - elif rotfin == 7.5: - nrot = [5.0,7.5,10.0] - else: - nrot = [rotfin-5.0,rotfin,rotfin+5.0] - - if Tfin == 3500: - nT = [3500,3750,4000] - elif Tfin == 7000: - nT = [6500,6750,7000] - else: - nT = [Tfin-250,Tfin,Tfin+250] - - if Gfin == 0.0: - nG = [0.0,0.5,1.0] - elif Gfin == 0.5: - nG = [0.0,0.5,1.0] - elif Gfin == 4.5: - nG = [4.0,4.5,5.0] - elif Gfin == 5.0: - nG = [4.0,4.5,5.0] - else: - nG = [Gfin-0.5,Gfin,Gfin+0.5] - - if Zfin == -2.5: - nZ = [-2.5,-2.0,-1.5] - elif Zfin == 0.5: - nZ = [0.0,0.2,0.5] - elif Zfin == 0.2: - nZ = [0.0,0.2,0.5] - elif Zfin == 0.0: - nZ = [-0.5,0.0,0.2,0.5] - else: - nZ = [Zfin-0.5,Zfin,Zfin+0.5] - - for v in nrot: - model_path2 = model_path+'vsini_'+str(v)+'/' - names = [] - calc = numpy.array(calculated) - - for t in nT: - for g in nG: - for z in nZ: - nam = 'R_'+str(v)+'_'+get_name(t,g,z) - I = numpy.where(calc == nam)[0] - if len(I)==0 and os.access(model_path2+nam,os.F_OK): - names.append(nam) - - for fits in names: - calculated.append(fits) - hd = pyfits.getheader(model_path2+fits) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path2+fits) - FF = sc[It] - mwa = wat*(1+velfin/lux) - - NCCF = corr(L,F,mwa,FF,ons) - - vecT.append(T) - vecG.append(G) - vecZ.append(Z) - vecCF.append(NCCF) - vecR.append(v) - #print T,G,Z,NCCF - - if NCCF > maximo: - mejor = True - maximo = NCCF - Tfin = T - Gfin = G - Zfin = Z - rotfin = v - - #print Tfin,Gfin,Zfin,rotfin,maximo - maximCCF = maximo - - total_inter = 1 - if total_inter == 0: - deltaV = 0.5 - deltaG = 0.05 - deltaT = 50.0 - deltaZ = 0.1 - - vCF = numpy.array(vecCF) - vvT = numpy.array(vecT) - vvG = numpy.array(vecG) - vvZ = numpy.array(vecZ) - vvV = numpy.array(vecR) - - ejeT = numpy.arange(numpy.min(nT),numpy.max(nT)+deltaT,deltaT) - ejeV = numpy.arange(numpy.min(nrot),numpy.max(nrot)+deltaV,deltaV) - ejeG = numpy.arange(numpy.min(nG),numpy.max(nG)+deltaG,deltaG) - ejeZ = numpy.arange(numpy.min(nZ),numpy.max(nZ)+deltaZ,deltaZ) - - lejeV = len(ejeV) - lejeG = len(ejeG) - lejeT = len(ejeT) - lejeZ = len(ejeZ) - - matCCF = numpy.zeros([lejeT,lejeG,lejeZ,lejeV],float) - - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for z in nZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - for g in nG: - posG = int(round((g-ejeG[0])/deltaG)) - I = numpy.where((vvV == v) & (vvG ==g) & (vvZ == z))[0] - if len(I) > 0: - vTt,vCFt = orden(vvT[I],vCF[I]) - - if len(I)>3: - tck = interpolate.splrep(vTt,vCFt,k=3,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - else: - ynew = numpy.zeros(len(ejeT),float)+vCFt[0] - - matCCF[:,posG,posZ,posV]=ynew - - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for z in nZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - y1 = matCCF[posT,:,posZ,posV] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeG[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - else: - ynew = numpy.zeros(len(ejeG),float)+y1b[0] - matCCF[posT,:,posZ,posV]=ynew - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - for g in ejeG: - posG = int(round((g-ejeG[0])/deltaG)) - y1 = matCCF[posT,posG,:,posV] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeZ[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeZ,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeZ,tck,der=0) - else: - ynew = numpy.zeros(len(ejeZ),float)+y1b[0] - matCCF[posT,posG,:,posV]=ynew - - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - for g in ejeG: - posG = int(round((g-ejeG[0])/deltaG)) - for z in ejeZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - y1 = matCCF[posT,posG,posZ,:] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeV[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeV,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeV,tck,der=0) - else: - ynew = numpy.zeros(len(ejeV),float)+y1b[0] - matCCF[posT,posG,posZ,:]=ynew - - I = numpy.where(matCCF == numpy.max(matCCF)) - - intert = ejeT[I[0]] - interg = ejeG[I[1]] - interz = ejeZ[I[2]] - interv = ejeV[I[3]] - maximCCF = numpy.max(CCF) - #print 'interp',intert,interg,interz,interv - - elif total_inter == 1: - if Tfin == 3500: - nT = [3500,3750,4000,4250] - elif Tfin == 3750: - nT = [3500,3750,4000,4250] - elif Tfin == 7000: - nT = [6250,6500,6750,7000] - elif Tfin == 6750: - nT = [6250,6500,6750,7000] - else: - nT = [Tfin-500,Tfin-250,Tfin,Tfin+250,Tfin+500] - - if Gfin == 0.0: - nG = [0.0,0.5,1.0,1.5] - elif Gfin == 0.5: - nG = [0.0,0.5,1.0,1.5] - elif Gfin == 4.5: - nG = [3.5,4.0,4.5,5.0] - elif Gfin == 5.0: - nG = [3.5,4.0,4.5,5.0] - else: - nG = [Gfin-1.0,Gfin-0.5,Gfin,Gfin+0.5,Gfin+1.0] - calc = numpy.array(calculated) - model_path2 = model_path+'vsini_'+str(rotfin)+'/' - names = [] - - for t in nT: - for g in nG: - nam = 'R_'+str(rotfin)+'_'+get_name(t,g,Zfin) - I = numpy.where(calc == nam)[0] - - if len(I) == 0 and os.access(model_path2+nam,os.F_OK): - names.append(nam) - - for fits in names: - calculated.append(fits) - hd = pyfits.getheader(model_path2+fits) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path2+fits) - FF = sc[It] - mwa = wat*(1+velfin/lux) - - NCCF = corr(L,F,mwa,FF,ons) - - vecZ.append(Z) - vecT.append(T) - vecG.append(G) - vecR.append(rotfin) - vecCF.append(NCCF) - - - VZ = numpy.array(vecZ) - VR = numpy.array(vecR) - VT = numpy.array(vecT) - VG = numpy.array(vecG) - VF = numpy.array(vecCF) - - I = numpy.where((VZ == Zfin) & (VR == rotfin))[0] - VT2 = VT[I] - VG2 = VG[I] - VF2 = VF[I] - deltaT = 50.0 - deltaG = 0.05 - - ejeT = numpy.arange(numpy.min(numpy.array(nT)),numpy.max(numpy.array(nT))+deltaT,deltaT) - ejeG = numpy.arange(numpy.min(numpy.array(nG)),numpy.max(numpy.array(nG))+deltaG,deltaG) - - lejeT = len(ejeT) - lejeG = len(ejeG) - - matCCF = numpy.zeros([lejeT,lejeG],float) - - for g in nG: - pos = int(round((g-ejeG[0])/deltaG)) - I = numpy.where(VG2 == g)[0] - if len(I) > 0: - - vTt,vCFt = orden(VT2[I],VF2[I]) - #print vTt,vCFt - if len(I)>3: - tck = interpolate.splrep(vTt,vCFt,k=3,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - else: - ynew = numpy.zeros(len(ejeT),float)+vCFt[0] - - matCCF[:,pos]=ynew - - for t in ejeT: - pos1 = int(round((t-ejeT[0])/deltaT)) - y1 = matCCF[pos1,:] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeG[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - else: - ynew = numpy.zeros(len(ejeG),float)+y1b[0] - matCCF[pos1,:]=ynew - - I = numpy.where(matCCF == numpy.max(matCCF)) - - intert = ejeT[I[0]][0] - interg = ejeG[I[1]][0] - interz = Zfin - interv = rotfin - #print intert,interg - maximCCF = numpy.max(matCCF) - - else: - intert,interg,interz,interv = Tfin,Gfin,Zfin,rotfin - #print 'HI' - if doplot: - nam = 'R_'+str(rotfin)+'_'+get_name(Tfin,Gfin,Zfin) - hd = pyfits.getheader(model_path2+nam) - sc = pyfits.getdata(model_path2+nam) - PWAV = ToVacuum(numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1']) - PWAV = PWAV*(1+velfin/lux) - for i in range(L1.shape[0]): - I = numpy.where((PWAV > L1[i,0]) & (PWAV < L1[i,-1]))[0] - #print L1[i] - #print F1[i] - #print i - f = figure() - ylim(0.,1.01) - plot(L1[i],F1[i]) - plot(PWAV[I],sc[I]) - xlabel('wavelenth [A]') - ylabel('Continuum Normalized Flux') - savefig(plot_dir + plot_name + '_' + str(int(L1[i,0])) + '_' + str(int(L1[i,-1])) + '.pdf', format='pdf') - - return [intert, interg, interz, interv, velfin,maximCCF] \ No newline at end of file + """ + This function finds an aproximation to the stellar parameters (Teff, log(g), [Fe/H]) + of the input echelle spectrum using a CCF with model spectra. This code also is + constructed to find the radial velocity of the star and v*sin(i). + """ + global It,L_Gl,F_Gl,ons,velo2,wat,lux + lux = 299792.458 + + L1,F1 = spec[0,:,:], spec[5,:,:] + for i in range(F1.shape[0]): + I = np.where(np.isnan(F1[i])==True)[0] + F1[i][I] = 1. + + #width_path = '/home/rabrahm/Desktop/corr2/' + #slines_path = '/home/rabrahm/Desktop/corr2/' + + SLi,SLf = numpy.loadtxt(base+'lines2.dat',dtype=None,unpack=True) + + SLi = ToVacuum(SLi) + SLf = ToVacuum(SLf) + + AT,AG,AZ,A000,A025,A050,A075,A100,A150,A200,A250,A300,A350,A400,A450,A500 =\ + numpy.loadtxt(base+'anchos50000.dat',dtype=None,unpack=True) + + vsini = [0.0,2.5,5.0,7.5,10.0,15.0,20.0,25.0,30.0,35.0,40.0,45.0,50.0] + + or01 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < Lf)[0] + if len(I) > 0: + or01 = i + break + + or02 = L1.shape[0]-1 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < Li)[0] + if len(I) > 0: + or02 = i + break + + or03 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 6250.0)[0] + if len(I) > 0: + or03 = i + break + + or04 = L1.shape[0]-1 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 5500.0)[0] + if len(I) > 0: + or04 = i + break + + or05 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 5190.0)[0] + W = numpy.where(L1[i] < 5170.0)[0] + if len(I) > 0 and len(W) > 0: + or05 = i + break + #print or01,or02,or03,or04,or05 + guess = [1.0,1.0,1.0] + + L = L1[or01:or02] + F = F1[or01:or02] + Lm = L1[or03:or04] + Fm = F1[or03:or04] + Lg = L1[or05] + Fg = F1[or05] + + + bad_lines = [[6860,6900],[6550,6580],[6270,6320],[4850,4880]] + ons = bad_orders(L,bad_lines) + + modi = 'R_0.0_5000_30_p00p00.ms.fits' + #print 'Radial velocity calculation via CCF with: '+modi + + sci = pyfits.getdata(model_path+'vsini_0.0/'+modi) + hdi = pyfits.getheader(model_path+'vsini_0.0/'+modi) + + wam1 = ToVacuum(numpy.arange(len(sci))*hdi['CD1_1']+hdi['CRVAL1']) + + Im = numpy.where((wam1 > 5400.0) & (wam1 < 6350.0))[0] + wam = wam1[Im] + flm = sci[Im] + + Ig = numpy.where((wam1 > 5100.0) & (wam1 < 5250.0))[0] + wag = wam1[Ig] + + It = numpy.where((wam1 > 4000.0) & (wam1 < 7500.0))[0] + wat = wam1[It] + + + for i in range(Lm.shape[0]): + I = numpy.where((Fm[i] != 0.0) & (Fm[i] < 2.0))[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I] + MFs = flm[I] + + vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200.0,200.0) + + if i == 0: + ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = Ls[-1]-Ls[0] + else: + ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + + ccf = ccf/nor + vv = numpy.array(vv) + + B = 0.5*(ccf[0]+ccf[-1]) + A = numpy.max(ccf)-B + med = vv[numpy.where(ccf == numpy.max(ccf))[0]] + sig = 20.0 + guess1 = [B,A,med,sig] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) + velo = ajustep[0][2] + + #print 'The initial radial velocity is: '+str(velo)+' km/s' + + #print 'Determining parameters of the initial model' + + vecti = [3500,4000,4500,5000,5500,6500] + vecgi = [0.0,1.5, 3.0,4.5] + #veczi = [-2.0,-1.0,0.0] + veczi = [-1.0,0.0] + ccmax = 0 + names = [] + cc = 0 + nor = 0 + for g in vecgi: + for z in veczi: + for t in vecti: + nam = get_name(t,g,z) + names.append(nam) + namemax = get_name(5500,4.5,0.0) + TEI = 5500 + MEI = 0.0 + LGI = 4.5 + for nam in names: + if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): + mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) + flm = mod[Im] + cc = 0.0 + nor = 0.0 + for o in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I]*(1+velo/lux) + MFs = flm[I] + + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + cc = cc + integrate.simps(NMFs[1:-1]*Fs[1:-1],Ls[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]))*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + cc = cc/nor + + if cc >=ccmax: + ccmax = cc + namemax = nam + + mod = pyfits.getheader(model_path+'vsini_0.0/R_0.0_'+namemax) + TEI = mod['TEFF'] + + #print 'Teff (initial) = '+str(TEI)+' K' + + + + if TEI <= 4000: + rot = 5.0 + LGI = 3.0 + MTI = 0.0 + late = True + velo2 = velo + + else: + late = False + t = TEI + vecgi = [1.0,2.0,3.0,4.0] + #veczi = [-2.0,-1.0,0.0] + veczi = [-1.0,0.0] + dif = 1000.0 + + for z in veczi: + vals = [] + di = 1000 + meg = 3.0 + for g in vecgi: + nam = get_name(t,g,z) + if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): + mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) + flm = mod[Im] + flm = el_stl(wam,flm,SLi,SLf) + intfl = 0.0 + intflm = 0.0 + di = 1000 + for o in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I]*(1+velo/lux) + MFs = flm[I] + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + intfl = intfl + integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1]) + intflm = intflm + integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]) + intfl = math.sqrt(intfl) + intflm = math.sqrt(intflm) + dif1 = math.sqrt((intflm-intfl)**2) + vals.append(dif1) + if dif1 < di: + di = dif1 + meg = g + dif2 = numpy.mean(vals) + #print z,dif2 + if dif2 Ls[0]-5.0) & (wag < Ls[-1]+5.0))[0] + MLs = wag[I]*(1+velo/lux) + MFs = mflg[I] + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + cc2 = integrate.simps(Fs[1:-1]*NMFs[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1])) + #print g,cc2 + if cc2 > cfi2: + cfi2 = cc2 + LGI = g + + #print 'Log(g) (initial) = '+str(LGI) + + itera = 0 + maximo = 0 + calculated = [] + rotss = [] + vecR = [] + vecT = [] + vecG = [] + + vecZ = [] + vecCF = [] + while itera < 4: + + if late==False: + + MOG = get_name(TEI,LGI,MEI) + sc = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+MOG) + + #print '-Calculating radial shift and v*sin(i) with model: '+MOG + + flm = sc[Im] + ies = [] + flm = el_stl(wam,flm,SLi,SLf) + for i in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + ies.append(I) + MLs = wam[I] + MFs = flm[I] + vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200,200) + if i == 0: + ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = Ls[-1]-Ls[0] + else: + ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + nor2=1. + ccf = ccf/nor + vv = numpy.array(vv) + B = 0.5*(ccf[0]+ccf[-1]) + A = numpy.max(ccf)-B + med = vv[numpy.where(ccf == numpy.max(ccf))[0]] + sig = 20.0 + guess1 = [B,A,med,sig] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) + velo2 = ajustep[0][2] + sig2 = ajustep[0][3] + sig2 = math.sqrt(sig2*sig2) + #plt.plot(vv,ccf,vv,gauss1(ajustep[0],vv)) + #plt.show() + #print 'radial velocity = '+str(velo2)+' km/s' + #print 'Sigma = '+str(sig2)+' km/s' + """ + + vi = 0.0 + difsigmin = 1000 + while vi <= 20.0: + ai = 0 + if ( (os.access(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG,os.F_OK) == True)): + modt = pyfits.getdata(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG) + flm2 = modt[Im] + flm2 = el_stl(wam,flm2,SLi,SLf) + for I in ies: + Fs = flm[I] + Ls = wam[I] + Fs = el_stl(Ls,Fs,SLi,SLf) + MFs = flm2[I] + vv2,cc2 = vels.CCF(Ls,MFs,Ls,Fs,-200,200) + if ai == 0: + ccf2 = numpy.array(cc2)*(Ls[-1]-Ls[0]) + nor2 = Ls[-1]-Ls[0] + else: + ccf2 = ccf2 + numpy.array(cc2)*(Ls[-1]-Ls[0]) + nor2 = nor2 + (Ls[-1]-Ls[0]) + ai += 1 + cc2 = ccf2/nor2 + vv2 = numpy.array(vv2) + B3 = 0.5*(cc2[0]+cc2[-1]) + A3 = numpy.max(cc2)-B3 + med3 = 0.0 + sig3 = 20.0 + guess1 = [B3,A3,med3,sig3] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) + cte3 = ajustep[0][0] + no3 = ajustep[0][1] + med3 = ajustep[0][2] + sig3 = ajustep[0][3] + + #plt.plot(vv2,cc2) + + print vi,sig3 + difsig = math.sqrt((sig2-sig3)**2) + if difsig < difsigmin: + difsigmin = difsig + rot = vi + ai +=1 + + if vi <= 7.5: + vi = vi+2.5 + elif vi < 50.0: + vi = vi+5.0 + else: + break + + #plt.show() + """ + if sig2 >= vsini[0]: + I = numpy.where((AT == TEI) & (AG == LGI) & (AZ == MEI))[0][0] + anchos = numpy.array([A000[I],A025[I],A050[I],A075[I],A100[I],A150[I],A200[I],A250[I],A300[I],A350[I],A400[I],A450[I],A500[I]]) + kis = 0 + while kis < len(anchos)-1: + if anchos[kis]>anchos[kis+1]: + portemp = anchos[kis] + anchos[kis] = anchos[kis+1] + anchos[kis+1] = portemp + kis+=1 + tck = interpolate.splrep(anchos,numpy.array(vsini),k=3,s=0) + calrot = interpolate.splev(sig2,tck,der=0) + difs = (numpy.array(vsini) - calrot)**2 + AI = int(numpy.where(difs == numpy.min(difs))[0]) + rot = vsini[AI] + else: + rot = vsini[0] + calrot = 0.0 + + #rot = 2.5 + #print 'v*sin(i) = '+str(calrot)+' km/s' + + RI = numpy.where(numpy.array(rotss) == rot)[0] + + if len(RI) > 0: + break + + else: + + model_path1 = model_path+"vsini_"+str(rot)+"/" + + """ conociendo la velocidad radial realizo una busqueda gruesa de parametros estelares calculando el maximo de la CCF""" + #print "-Searching the optimal stellar model" + + #maxT,maxG,maxZ,maxCor,maxvs = 5500,4.0,0.0,0.0,0.0 + maxCor = 0.0 + + if TEI >= 3500 and TEI <=4250: + modT = [3500,4000,4500,5000] + else: + modT = [4000,4500,5000,5500,6000,6500,7000] + + modG = [1.0,2.5,4.0] + #modZ = [-2.0,-1.0,0.0] + modZ = [-1.0,0.0] + MOD,MOD2 = [],[] + + i=0 + while i < len(modZ): + ig = 0 + while ig < len(modG): + it = 0 + while it < len(modT): + MK = 'R_'+str(rot)+'_'+get_name(modT[it],modG[ig],modZ[i]) + if os.access(model_path1+MK,os.F_OK): + MOD.append(MK) + MOD2.append(model_path1+MK) + it = it+1 + ig =ig+1 + i = i+1 + + MOD = np.array(MOD) + MOD2 = np.array(MOD2) + + all_pars = np.vstack((MOD,np.zeros(len(MOD))+velo2)) + L_Gl,F_Gl =L.copy(),F.copy() + p = Pool(npools) + xc_values = np.array((p.map(corr_p, MOD2))) + p.terminate() + I_min = np.argmax(xc_values) + hd = pyfits.getheader(MOD2[I_min]) + maxCor = xc_values[I_min] + maxG = hd['LOG_G'] + maxT = hd['TEFF'] + maxZ = hd['FEH'] + maxvs = hd['VSINI'] + #print MOD2[I_min] + """ + print xc_values + print MOD2[I_min],I_min + print dfgh + + for m in MOD: + + hd = pyfits.getheader(model_path1+m) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + vs = hd['VSINI'] + + sc = pyfits.getdata(model_path1+m) + FF = sc[It] + mwa = wat*(1+velo2/lux) + NCCF = corr(L,F,mwa,FF,ons) + + #print T,G,Z,vs,NCCF + + if NCCF > maxCor: + maxCor = NCCF + maxG = G + maxT = T + maxZ = Z + maxvs = vs + elif NCCF<0: + print "Problem with spectrum!!! -> Negative value of CCF." + maxG = 4.5 + maxT = 5500 + maxZ = 0 + maxvs = 0 + """ + + + + #print 'maxgrueso',maxT,maxG,maxZ,maxvs + + + """ A partir de los parametros encontrados en la busqueda gruesa procedo a constrenir los limites de la busqueda fina""" + + + #Tfin,Gfin,Zfin,rotfin,velfin = 5500,4.0,0.0,0.0,0.0 + """ Ahora se buscan los par'ametros estelares optimos mediante una exploracion fina""" + + if maxT == 3500 or maxT == 4000: + modT = [3500,3750,4000,4250,4500] + + elif maxT == 7000 or maxT == 6500: + modT = [6000,6250,6500,6750,7000] + else: + modT = [maxT-750,maxT-500,maxT-250,maxT,maxT+250,maxT+500,maxT+750] + + if maxG == 1.0: + modG = [0.0,0.5,1.0,1.5,2.0,2.5] + if maxG == 2.5: + modG = [1.0,1.5,2.0,2.5,3.0,3.5,4.0] + if maxG == 4.0: + modG = [2.5,3.0,3.5,4.0,4.5,5.0] + + if maxZ == -2.0: + modZ = [-2.5,-2.0,-1.5,-1.0] + if maxZ == -1.0: + modZ = [-1.5,-1.0,-0.5,0.0] + if maxZ == 0.0: + modZ = [-0.5,0.0,0.2,0.5] + + MOD,MOD2 = [],[] + for i in modZ: + for ig in modG: + for it in modT: + MK = 'R_'+str(rot)+'_'+get_name(it,ig,i) + if os.access(model_path1+MK,os.F_OK): + MOD.append(MK) + MOD2.append(model_path1+MK) + MOD2 = np.array(MOD2) + + L_Gl,F_Gl =L.copy(),F.copy() + p = Pool(npools) + xc_values = np.array((p.map(corr_p, MOD2))) + p.terminate() + I_min = np.argmax(xc_values) + hd = pyfits.getheader(MOD2[I_min]) + maxCor = xc_values[I_min] + maxG = hd['LOG_G'] + maxT = hd['TEFF'] + maxZ = hd['FEH'] + maxvs = hd['VSINI'] + #print MOD2[I_min] + """ + #maxT,maxG,maxZ,maxCor = 5500,4.0,0,0 + maxCor = 0.0 + for m in MOD: + calculated.append(m) + hd = pyfits.getheader(model_path1+m) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path1+m) + FF = sc[It] + mwa = wat*(1+velo2/lux) + + NCCF = corr(L,F,mwa,FF,ons) + + vecT.append(T) + vecG.append(G) + vecZ.append(Z) + vecR.append(rot) + vecCF.append(NCCF) + + #print T,G,Z,NCCF + + if NCCF > maxCor: + maxCor = NCCF + maxT = T + maxG = G + maxZ = Z + + elif NCCF < 0: + print "Problem with spectrum!!! -> Negative value of CCF." + maxG = 4.5 + maxT = 5500 + maxZ = 0 + maxvs = 0 + """ + #print "maxfino",maxT,maxG,maxZ,maxCor + + TEI = maxT + LGI = maxG + MEI = maxZ + + ultrot = rot + + if maxCor > maximo: + maximo = maxCor + Tfin,Gfin,Zfin,rotfin,velfin = maxT,maxG,maxZ,rot,velo2 + #Tf,Gf,Zf = intert,interg,maZ + + late = False + rotss.append(rot) + + + itera = itera+1 + if maximo == 0: + Tfin,Gfin,Zfin,rotfin,velfin = 5500, 4.5,0,0,velo2 + #print 'Pars fase 0:', Tfin, Gfin, Zfin,rotfin,maximo + mejor = False + if rotfin == 0.0: + nrot = [0.0,2.5] + elif rotfin == 2.5: + nrot = [0.0,2.5,5.0] + elif rotfin == 5.0: + nrot = [2.5,5.0,7.5] + elif rotfin == 7.5: + nrot = [5.0,7.5,10.0] + else: + nrot = [rotfin-5.0,rotfin,rotfin+5.0] + + if Tfin == 3500: + nT = [3500,3750,4000] + elif Tfin == 7000: + nT = [6500,6750,7000] + else: + nT = [Tfin-250,Tfin,Tfin+250] + + if Gfin == 0.0: + nG = [0.0,0.5,1.0] + elif Gfin == 0.5: + nG = [0.0,0.5,1.0] + elif Gfin == 4.5: + nG = [4.0,4.5,5.0] + elif Gfin == 5.0: + nG = [4.0,4.5,5.0] + else: + nG = [Gfin-0.5,Gfin,Gfin+0.5] + + if Zfin == -2.5: + nZ = [-2.5,-2.0,-1.5] + elif Zfin == 0.5: + nZ = [0.0,0.2,0.5] + elif Zfin == 0.2: + nZ = [0.0,0.2,0.5] + elif Zfin == 0.0: + nZ = [-0.5,0.0,0.2,0.5] + else: + nZ = [Zfin-0.5,Zfin,Zfin+0.5] + + for v in nrot: + model_path2 = model_path+'vsini_'+str(v)+'/' + names = [] + calc = numpy.array(calculated) + + for t in nT: + for g in nG: + for z in nZ: + nam = 'R_'+str(v)+'_'+get_name(t,g,z) + I = numpy.where(calc == nam)[0] + if len(I)==0 and os.access(model_path2+nam,os.F_OK): + names.append(nam) + + for fits in names: + calculated.append(fits) + hd = pyfits.getheader(model_path2+fits) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path2+fits) + FF = sc[It] + mwa = wat*(1+velfin/lux) + + NCCF = corr(L,F,mwa,FF,ons) + + vecT.append(T) + vecG.append(G) + vecZ.append(Z) + vecCF.append(NCCF) + vecR.append(v) + #print T,G,Z,NCCF + + if NCCF > maximo: + mejor = True + maximo = NCCF + Tfin = T + Gfin = G + Zfin = Z + rotfin = v + + #print Tfin,Gfin,Zfin,rotfin,maximo + maximCCF = maximo + + total_inter = 1 + if total_inter == 0: + deltaV = 0.5 + deltaG = 0.05 + deltaT = 50.0 + deltaZ = 0.1 + + vCF = numpy.array(vecCF) + vvT = numpy.array(vecT) + vvG = numpy.array(vecG) + vvZ = numpy.array(vecZ) + vvV = numpy.array(vecR) + + ejeT = numpy.arange(numpy.min(nT),numpy.max(nT)+deltaT,deltaT) + ejeV = numpy.arange(numpy.min(nrot),numpy.max(nrot)+deltaV,deltaV) + ejeG = numpy.arange(numpy.min(nG),numpy.max(nG)+deltaG,deltaG) + ejeZ = numpy.arange(numpy.min(nZ),numpy.max(nZ)+deltaZ,deltaZ) + + lejeV = len(ejeV) + lejeG = len(ejeG) + lejeT = len(ejeT) + lejeZ = len(ejeZ) + + matCCF = numpy.zeros([lejeT,lejeG,lejeZ,lejeV],float) + + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for z in nZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + for g in nG: + posG = int(round((g-ejeG[0])/deltaG)) + I = numpy.where((vvV == v) & (vvG ==g) & (vvZ == z))[0] + if len(I) > 0: + vTt,vCFt = orden(vvT[I],vCF[I]) + + if len(I)>3: + tck = interpolate.splrep(vTt,vCFt,k=3,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + else: + ynew = numpy.zeros(len(ejeT),float)+vCFt[0] + + matCCF[:,posG,posZ,posV]=ynew + + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for z in nZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + y1 = matCCF[posT,:,posZ,posV] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeG[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + else: + ynew = numpy.zeros(len(ejeG),float)+y1b[0] + matCCF[posT,:,posZ,posV]=ynew + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + for g in ejeG: + posG = int(round((g-ejeG[0])/deltaG)) + y1 = matCCF[posT,posG,:,posV] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeZ[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeZ,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeZ,tck,der=0) + else: + ynew = numpy.zeros(len(ejeZ),float)+y1b[0] + matCCF[posT,posG,:,posV]=ynew + + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + for g in ejeG: + posG = int(round((g-ejeG[0])/deltaG)) + for z in ejeZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + y1 = matCCF[posT,posG,posZ,:] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeV[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeV,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeV,tck,der=0) + else: + ynew = numpy.zeros(len(ejeV),float)+y1b[0] + matCCF[posT,posG,posZ,:]=ynew + + I = numpy.where(matCCF == numpy.max(matCCF)) + + intert = ejeT[I[0]] + interg = ejeG[I[1]] + interz = ejeZ[I[2]] + interv = ejeV[I[3]] + maximCCF = numpy.max(CCF) + #print 'interp',intert,interg,interz,interv + + elif total_inter == 1: + if Tfin == 3500: + nT = [3500,3750,4000,4250] + elif Tfin == 3750: + nT = [3500,3750,4000,4250] + elif Tfin == 7000: + nT = [6250,6500,6750,7000] + elif Tfin == 6750: + nT = [6250,6500,6750,7000] + else: + nT = [Tfin-500,Tfin-250,Tfin,Tfin+250,Tfin+500] + + if Gfin == 0.0: + nG = [0.0,0.5,1.0,1.5] + elif Gfin == 0.5: + nG = [0.0,0.5,1.0,1.5] + elif Gfin == 4.5: + nG = [3.5,4.0,4.5,5.0] + elif Gfin == 5.0: + nG = [3.5,4.0,4.5,5.0] + else: + nG = [Gfin-1.0,Gfin-0.5,Gfin,Gfin+0.5,Gfin+1.0] + calc = numpy.array(calculated) + model_path2 = model_path+'vsini_'+str(rotfin)+'/' + names = [] + + for t in nT: + for g in nG: + nam = 'R_'+str(rotfin)+'_'+get_name(t,g,Zfin) + I = numpy.where(calc == nam)[0] + + if len(I) == 0 and os.access(model_path2+nam,os.F_OK): + names.append(nam) + + for fits in names: + calculated.append(fits) + hd = pyfits.getheader(model_path2+fits) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path2+fits) + FF = sc[It] + mwa = wat*(1+velfin/lux) + + NCCF = corr(L,F,mwa,FF,ons) + + vecZ.append(Z) + vecT.append(T) + vecG.append(G) + vecR.append(rotfin) + vecCF.append(NCCF) + + + VZ = numpy.array(vecZ) + VR = numpy.array(vecR) + VT = numpy.array(vecT) + VG = numpy.array(vecG) + VF = numpy.array(vecCF) + + I = numpy.where((VZ == Zfin) & (VR == rotfin))[0] + VT2 = VT[I] + VG2 = VG[I] + VF2 = VF[I] + deltaT = 50.0 + deltaG = 0.05 + + ejeT = numpy.arange(numpy.min(numpy.array(nT)),numpy.max(numpy.array(nT))+deltaT,deltaT) + ejeG = numpy.arange(numpy.min(numpy.array(nG)),numpy.max(numpy.array(nG))+deltaG,deltaG) + + lejeT = len(ejeT) + lejeG = len(ejeG) + + matCCF = numpy.zeros([lejeT,lejeG],float) + + for g in nG: + pos = int(round((g-ejeG[0])/deltaG)) + I = numpy.where(VG2 == g)[0] + if len(I) > 0: + + vTt,vCFt = orden(VT2[I],VF2[I]) + #print vTt,vCFt + if len(I)>3: + tck = interpolate.splrep(vTt,vCFt,k=3,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + else: + ynew = numpy.zeros(len(ejeT),float)+vCFt[0] + + matCCF[:,pos]=ynew + + for t in ejeT: + pos1 = int(round((t-ejeT[0])/deltaT)) + y1 = matCCF[pos1,:] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeG[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + else: + ynew = numpy.zeros(len(ejeG),float)+y1b[0] + matCCF[pos1,:]=ynew + + I = numpy.where(matCCF == numpy.max(matCCF)) + + intert = ejeT[I[0]][0] + interg = ejeG[I[1]][0] + interz = Zfin + interv = rotfin + #print intert,interg + maximCCF = numpy.max(matCCF) + + else: + intert,interg,interz,interv = Tfin,Gfin,Zfin,rotfin + #print 'HI' + if doplot: + nam = 'R_'+str(rotfin)+'_'+get_name(Tfin,Gfin,Zfin) + hd = pyfits.getheader(model_path2+nam) + sc = pyfits.getdata(model_path2+nam) + PWAV = ToVacuum(numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1']) + PWAV = PWAV*(1+velfin/lux) + for i in range(L1.shape[0]): + I = numpy.where((PWAV > L1[i,0]) & (PWAV < L1[i,-1]))[0] + #print L1[i] + #print F1[i] + #print i + f = figure() + ylim(0.,1.01) + plot(L1[i],F1[i]) + plot(PWAV[I],sc[I]) + xlabel('wavelenth [A]') + ylabel('Continuum Normalized Flux') + savefig(plot_dir + plot_name + '_' + str(int(L1[i,0])) + '_' + str(int(L1[i,-1])) + '.pdf', format='pdf') + + return [intert, interg, interz, interv, velfin,maximCCF] diff --git a/utils/Correlation/correlation2.py b/utils/Correlation/correlation2.py index 40485d1..7e27b97 100644 --- a/utils/Correlation/correlation2.py +++ b/utils/Correlation/correlation2.py @@ -1,3 +1,4 @@ +from __future__ import print_function #!/usr/bin/python from pylab import * base = '/data/echelle/' @@ -15,8 +16,8 @@ from pylab import * def n_Edlen(l): - """ - Refractive index according to Edlen 1966 + """ + Refractive index according to Edlen 1966 """ @@ -37,7 +38,7 @@ def n_Morton(l): return n def ToAir(l): - """ + """ Transforms input wavelength (A) from Vacuum to Air Uses refractive index as per Edlen 1966 @@ -47,7 +48,7 @@ def ToAir(l): return (l / n_Edlen(l)) def ToVacuum(l): - """ + """ Transforms input wavelength (A) from Air to Vacuum Uses refractive index as per Edlen 1966 @@ -61,1095 +62,1095 @@ def ToVacuum(l): l_new = n_Edlen(l_prev) * l if (max(abs(l_new - l_prev)) < 1e-10): cond = 0 l_prev = l_new - + return l_prev def el_stl(wam,fl,SLi,SLf): - for i in range(len(SLi)): - if SLi[i]>wam[-1]: - break - I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] - fl[I]=1.0 - return fl + for i in range(len(SLi)): + if SLi[i]>wam[-1]: + break + I = numpy.where((wam >= SLi[i]) & (wam<=SLf[i]))[0] + fl[I]=1.0 + return fl def corr(L,F,mwa,FF,ons): - CCF,NOR = 0,0 - - o = 0 - while o < L.shape[0]: - k = 0 - while k 0: - Fl = F[o][I] - Ll = L[o][I] - - I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] - MML = mwa[I] - MF = FF[I] - #ajustec = continuum.NORM_single(MML, MF, orden=2) - #plot( MML, MF ) - #plot( MML, numpy.polyval(ajustec, MML)) - - #NMF = MF / numpy.polyval(ajustec, MML) - tckM = interpolate.splrep(MML,MF,k=3,s=0) - NFM = interpolate.splev(Ll,tckM,der=0) - - a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) - b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) - c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) - - CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) - NOR = NOR+Ll[-2]-Ll[1] - - o += 1 - #show() - return CCF/NOR + CCF,NOR = 0,0 + + o = 0 + while o < L.shape[0]: + k = 0 + while k 0: + Fl = F[o][I] + Ll = L[o][I] + + I = numpy.where( (mwa > Ll[0]) & (mwa< Ll[-1]) )[0] + MML = mwa[I] + MF = FF[I] + #ajustec = continuum.NORM_single(MML, MF, orden=2) + #plot( MML, MF ) + #plot( MML, numpy.polyval(ajustec, MML)) + + #NMF = MF / numpy.polyval(ajustec, MML) + tckM = interpolate.splrep(MML,MF,k=3,s=0) + NFM = interpolate.splev(Ll,tckM,der=0) + + a = integrate.simps(Fl[1:-1]*NFM[1:-1],Ll[1:-1]) + b = integrate.simps(Fl[1:-1]*Fl[1:-1],Ll[1:-1]) + c = integrate.simps(NFM[1:-1]*NFM[1:-1],Ll[1:-1]) + + CCF = CCF+(a/math.sqrt(b*c))*(Ll[-2]-Ll[1]) + NOR = NOR+Ll[-2]-Ll[1] + + o += 1 + #show() + return CCF/NOR def get_con_coef(MML,MDR,n): - Io = len(MML) - coef = numpy.polyfit(MML,MDR,n) - res = MDR - numpy.polyval(coef,MML) - dev = numpy.sqrt(numpy.var( res )) - J = numpy.where( numpy.absolute(res) > 3*dev)[0] - I = numpy.where( numpy.absolute(res) <= 3*dev)[0] - cond = True - if len(J) == 0 or len(I)< 0.3*Io: - cond = False - while cond: - MML,MDR = MML[I],MDR[I] - coef = numpy.polyfit(MML,MDR,n) - res = MDR - numpy.polyval(coef,MML) - dev = numpy.sqrt(numpy.var( res )) - J = numpy.where( numpy.absolute(res) > 3*dev)[0] - I = numpy.where( numpy.absolute(res) <= 3*dev)[0] - if len(J) == 0 or len(I)< 0.3*Io: - cond = False - return coef + Io = len(MML) + coef = numpy.polyfit(MML,MDR,n) + res = MDR - numpy.polyval(coef,MML) + dev = numpy.sqrt(numpy.var( res )) + J = numpy.where( numpy.absolute(res) > 3*dev)[0] + I = numpy.where( numpy.absolute(res) <= 3*dev)[0] + cond = True + if len(J) == 0 or len(I)< 0.3*Io: + cond = False + while cond: + MML,MDR = MML[I],MDR[I] + coef = numpy.polyfit(MML,MDR,n) + res = MDR - numpy.polyval(coef,MML) + dev = numpy.sqrt(numpy.var( res )) + J = numpy.where( numpy.absolute(res) > 3*dev)[0] + I = numpy.where( numpy.absolute(res) <= 3*dev)[0] + if len(J) == 0 or len(I)< 0.3*Io: + cond = False + return coef def get_chis(L,F,snr,mwa,FF,ons): - res = numpy.array([]) - o = 0 - while o < L.shape[0]: - k = 0 - while k 0: - Fl = F[o][I] - Ll = L[o][I] - SN = snr[o][I] - I = numpy.where( (mwa > L[o][0]) & (mwa< L[o][-1]) )[0] - MML = mwa[I] - MF = FF[I] - #ajustec = continuum.NORM_single(MML, MF, orden=2) - #plot( MML, MF ) - #plot( MML, numpy.polyval(ajustec, MML)) - - #NMF = MF / numpy.polyval(ajustec, MML) - tckM = interpolate.splrep(MML,MF,k=3,s=0) - NFM = interpolate.splev(Ll,tckM,der=0) - - MDR = NFM/Fl - coef = get_con_coef(Ll,MDR,3) - Fl = Fl*numpy.polyval(coef,Ll) - #plot(Ll,NFM,linewidth=2.0) - #plot(Ll,Fl) - err = numpy.polyval(coef,Ll)/SN - res = numpy.hstack((res,(Fl - NFM)/err)) - o+=1 - chis = numpy.add.reduce(res**2)/float(len(res)-5) - - #show() - - return chis + res = numpy.array([]) + o = 0 + while o < L.shape[0]: + k = 0 + while k 0: + Fl = F[o][I] + Ll = L[o][I] + SN = snr[o][I] + I = numpy.where( (mwa > L[o][0]) & (mwa< L[o][-1]) )[0] + MML = mwa[I] + MF = FF[I] + #ajustec = continuum.NORM_single(MML, MF, orden=2) + #plot( MML, MF ) + #plot( MML, numpy.polyval(ajustec, MML)) + + #NMF = MF / numpy.polyval(ajustec, MML) + tckM = interpolate.splrep(MML,MF,k=3,s=0) + NFM = interpolate.splev(Ll,tckM,der=0) + + MDR = NFM/Fl + coef = get_con_coef(Ll,MDR,3) + Fl = Fl*numpy.polyval(coef,Ll) + #plot(Ll,NFM,linewidth=2.0) + #plot(Ll,Fl) + err = numpy.polyval(coef,Ll)/SN + res = numpy.hstack((res,(Fl - NFM)/err)) + o+=1 + chis = numpy.add.reduce(res**2)/float(len(res)-5) + + #show() + + return chis def bad_orders(L,bl): - bo = [] - - o = 0 - while o < L.shape[0]: - i=0 - while ibl[i][0]) & (L[o]bl[i][0]) & (L[o] 0: - or01 = i - break - - or02 = L1.shape[0]-1 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 4500.0)[0] - if len(I) > 0: - or02 = i - break - - or03 = 0 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 6250.0)[0] - if len(I) > 0: - or03 = i - break - - or04 = L1.shape[0]-1 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 5500.0)[0] - if len(I) > 0: - or04 = i - break - - or05 = 0 - for i in range(L1.shape[0]): - I = numpy.where(L1[i] < 5190.0)[0] - W = numpy.where(L1[i] < 5170.0)[0] - if len(I) > 0 and len(W) > 0: - or05 = i - break - #print or01,or02,or03,or04,or05 - guess = [1.0,1.0,1.0] - - L = L1[or01:or02] - F = F1[or01:or02] - SNR = SNR1[or01:or02] - Lm = L1[or03:or04] - Fm = F1[or03:or04] - SNRm = SNR1[or03:or04] - Lg = L1[or05] - Fg = F1[or05] - SNRg = SNR1[or05] - - bad_lines = [[6860,6900],[6550,6580],[6270,6320],[4850,4880]] - ons = bad_orders(L,bad_lines) - - modi = 'R_0.0_5000_30_p00p00.ms.fits' - #print 'Radial velocity calculation via CCF with: '+modi - - sci = pyfits.getdata(model_path+'vsini_0.0/'+modi) - hdi = pyfits.getheader(model_path+'vsini_0.0/'+modi) - - wam1 = ToVacuum(numpy.arange(len(sci))*hdi['CD1_1']+hdi['CRVAL1']) - - Im = numpy.where((wam1 > 5400.0) & (wam1 < 6350.0))[0] - wam = wam1[Im] - flm = sci[Im] - - Ig = numpy.where((wam1 > 5100.0) & (wam1 < 5250.0))[0] - wag = wam1[Ig] - - It = numpy.where((wam1 > 4000.0) & (wam1 < 7500.0))[0] - wat = wam1[It] - - for i in range(Lm.shape[0]): - I = numpy.where((Fm[i] != 0.0) & (Fm[i] < 2.0))[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I] - MFs = flm[I] - - vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200.0,200.0) - - if i == 0: - ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = Ls[-1]-Ls[0] - else: - ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - - - ccf = ccf/nor - vv = numpy.array(vv) - - B = 0.5*(ccf[0]+ccf[-1]) - A = numpy.max(ccf)-B - med = vv[numpy.where(ccf == numpy.max(ccf))[0]] - sig = 20.0 - guess1 = [B,A,med,sig] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) - velo = ajustep[0][2] - - #print 'The initial radial velocity is: '+str(velo)+' km/s' - - #print 'Determining parameters of the initial model' - - vecti = [3500,4000,4500,5000,5500,6500] - vecgi = [0.0,1.5, 3.0,4.5] - #veczi = [-2.0,-1.0,0.0] - veczi = [-1.0,0.0] - ccmax = 0 - names = [] - cc = 0 - nor = 0 - for g in vecgi: - for z in veczi: - for t in vecti: - nam = get_name(t,g,z) - names.append(nam) - namemax = get_name(5500,4.5,0.0) - TEI = 5500 - MEI = 0.0 - LGI = 4.5 - for nam in names: - if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): - mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) - flm = mod[Im] - cc = 0.0 - nor = 0.0 - for o in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I]*(1+velo/lux) - MFs = flm[I] - - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - cc = cc + integrate.simps(NMFs[1:-1]*Fs[1:-1],Ls[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]))*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - cc = cc/nor - - if cc >=ccmax: - ccmax = cc - namemax = nam - - mod = pyfits.getheader(model_path+'vsini_0.0/R_0.0_'+namemax) - TEI = mod['TEFF'] - - #print 'Teff (initial) = '+str(TEI)+' K' - - - - if TEI <= 4000: - rot = 5.0 - LGI = 3.0 - MTI = 0.0 - late = True - velo2 = velo - - else: - late = False - t = TEI - vecgi = [1.0,2.0,3.0,4.0] - #veczi = [-2.0,-1.0,0.0] - veczi = [-1.0,0.0] - dif = 1000.0 - - for z in veczi: - vals = [] - di = 1000 - meg = 3.0 - for g in vecgi: - nam = get_name(t,g,z) - if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): - mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) - flm = mod[Im] - flm = el_stl(wam,flm,SLi,SLf) - intfl = 0.0 - intflm = 0.0 - di = 1000 - for o in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - MLs = wam[I]*(1+velo/lux) - MFs = flm[I] - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - intfl = intfl + integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1]) - intflm = intflm + integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]) - intfl = math.sqrt(intfl) - intflm = math.sqrt(intflm) - dif1 = math.sqrt((intflm-intfl)**2) - vals.append(dif1) - if dif1 < di: - di = dif1 - meg = g - dif2 = numpy.mean(vals) - #print z,dif2 - if dif2 Ls[0]-5.0) & (wag < Ls[-1]+5.0))[0] - MLs = wag[I]*(1+velo/lux) - MFs = mflg[I] - tck = interpolate.splrep(MLs,MFs,k=3,s=0) - NMFs = interpolate.splev(Ls,tck,der=0) - - cc2 = integrate.simps(Fs[1:-1]*NMFs[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1])) - #print g,cc2 - if cc2 > cfi2: - cfi2 = cc2 - LGI = g - - #print 'Log(g) (initial) = '+str(LGI) - - itera = 0 - maximo = 0 - calculated = [] - rotss = [] - vecR = [] - vecT = [] - vecG = [] - - vecZ = [] - vecCF = [] - while itera < 4: - - if late==False: - - MOG = get_name(TEI,LGI,MEI) - sc = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+MOG) - - #print '-Calculating radial shift and v*sin(i) with model: '+MOG - - flm = sc[Im] - ies = [] - flm = el_stl(wam,flm,SLi,SLf) - for i in range(Lm.shape[0]): - I = numpy.where(Fm[i] != 0.0)[0] - Ls = Lm[i][I] - Fs = Fm[i][I] - Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) - I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] - ies.append(I) - MLs = wam[I] - MFs = flm[I] - vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200,200) - if i == 0: - ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = Ls[-1]-Ls[0] - else: - ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) - nor = nor + (Ls[-1]-Ls[0]) - nor2=1. - ccf = ccf/nor - vv = numpy.array(vv) - B = 0.5*(ccf[0]+ccf[-1]) - A = numpy.max(ccf)-B - med = vv[numpy.where(ccf == numpy.max(ccf))[0]] - sig = 20.0 - guess1 = [B,A,med,sig] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) - velo2 = ajustep[0][2] - sig2 = ajustep[0][3] - sig2 = math.sqrt(sig2*sig2) - #plt.plot(vv,ccf,vv,gauss1(ajustep[0],vv)) - #plt.show() - #print 'radial velocity = '+str(velo2)+' km/s' - #print 'Sigma = '+str(sig2)+' km/s' - """ - - vi = 0.0 - difsigmin = 1000 - while vi <= 20.0: - ai = 0 - if ( (os.access(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG,os.F_OK) == True)): - modt = pyfits.getdata(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG) - flm2 = modt[Im] - flm2 = el_stl(wam,flm2,SLi,SLf) - for I in ies: - Fs = flm[I] - Ls = wam[I] - Fs = el_stl(Ls,Fs,SLi,SLf) - MFs = flm2[I] - vv2,cc2 = vels.CCF(Ls,MFs,Ls,Fs,-200,200) - if ai == 0: - ccf2 = numpy.array(cc2)*(Ls[-1]-Ls[0]) - nor2 = Ls[-1]-Ls[0] - else: - ccf2 = ccf2 + numpy.array(cc2)*(Ls[-1]-Ls[0]) - nor2 = nor2 + (Ls[-1]-Ls[0]) - ai += 1 - cc2 = ccf2/nor2 - vv2 = numpy.array(vv2) - B3 = 0.5*(cc2[0]+cc2[-1]) - A3 = numpy.max(cc2)-B3 - med3 = 0.0 - sig3 = 20.0 - guess1 = [B3,A3,med3,sig3] - ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) - cte3 = ajustep[0][0] - no3 = ajustep[0][1] - med3 = ajustep[0][2] - sig3 = ajustep[0][3] - - #plt.plot(vv2,cc2) - - print vi,sig3 - difsig = math.sqrt((sig2-sig3)**2) - if difsig < difsigmin: - difsigmin = difsig - rot = vi - ai +=1 - - if vi <= 7.5: - vi = vi+2.5 - elif vi < 50.0: - vi = vi+5.0 - else: - break - - #plt.show() - """ - if sig2 >= vsini[0]: - I = numpy.where((AT == TEI) & (AG == LGI) & (AZ == MEI))[0][0] - anchos = numpy.array([A000[I],A025[I],A050[I],A075[I],A100[I],A150[I],A200[I],A250[I],A300[I],A350[I],A400[I],A450[I],A500[I]]) - kis = 0 - while kis < len(anchos)-1: - if anchos[kis]>anchos[kis+1]: - portemp = anchos[kis] - anchos[kis] = anchos[kis+1] - anchos[kis+1] = portemp - kis+=1 - tck = interpolate.splrep(anchos,numpy.array(vsini),k=3,s=0) - calrot = interpolate.splev(sig2,tck,der=0) - difs = (numpy.array(vsini) - calrot)**2 - AI = numpy.where(difs == numpy.min(difs))[0] - rot = vsini[AI] - else: - rot = vsini[0] - calrot = 0.0 - - #rot = 2.5 - #print 'v*sin(i) = '+str(calrot)+' km/s' - - RI = numpy.where(numpy.array(rotss) == rot)[0] - - if len(RI) > 0: - break - - else: - - model_path1 = model_path+"vsini_"+str(rot)+"/" - - """ conociendo la velocidad radial realizo una busqueda gruesa de parametros estelares calculando el maximo de la CCF""" - #print "-Searching the optimal stellar model" - - #maxT,maxG,maxZ,maxCor,maxvs = 5500,4.0,0.0,0.0,0.0 - maxCor = 0.0 - minCHIS = 100000000000000000000. - if TEI >= 3500 and TEI <=4250: - modT = [3500,4000,4500,5000] - else: - modT = [4000,4500,5000,5500,6000,6500,7000] - - modG = [1.0,2.5,4.0] - #modZ = [-2.0,-1.0,0.0] - modZ = [-1.0,0.0] - MOD = [] - - i=0 - while i < len(modZ): - ig = 0 - while ig < len(modG): - it = 0 - while it < len(modT): - MK = 'R_'+str(rot)+'_'+get_name(modT[it],modG[ig],modZ[i]) - if os.access(model_path1+MK,os.F_OK): - MOD.append(MK) - it = it+1 - ig =ig+1 - i = i+1 - - for m in MOD: - - hd = pyfits.getheader(model_path1+m) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - vs = hd['VSINI'] - - sc = pyfits.getdata(model_path1+m) - FF = sc[It] - mwa = wat*(1+velo2/lux) - - #NCCF = corr(L,F,mwa,FF,ons) - CHIS = get_chis(L,F,SNR,mwa,FF,ons) - - NCCF = 1./CHIS - if NCCF > maxCor: - maxCor = NCCF - maxG = G - maxT = T - maxZ = Z - maxvs = vs - elif NCCF<0: - #print "Problem with spectrum!!! -> Negative value of CCF." - maxG = 4.5 - maxT = 5500 - maxZ = 0 - maxvs = 0 - - - - #print 'maxgrueso',maxT,maxG,maxZ,maxvs - - """ A partir de los parametros encontrados en la busqueda gruesa procedo a constrenir los limites de la busqueda fina""" - - - #Tfin,Gfin,Zfin,rotfin,velfin = 5500,4.0,0.0,0.0,0.0 - """ Ahora se buscan los par'ametros estelares optimos mediante una exploracion fina""" - - if maxT == 3500 or maxT == 4000: - modT = [3500,3750,4000,4250,4500] - - elif maxT == 7000 or maxT == 6500: - modT = [6000,6250,6500,6750,7000] - else: - modT = [maxT-750,maxT-500,maxT-250,maxT,maxT+250,maxT+500,maxT+750] - - if maxG == 1.0: - modG = [0.0,0.5,1.0,1.5,2.0,2.5] - if maxG == 2.5: - modG = [1.0,1.5,2.0,2.5,3.0,3.5,4.0] - if maxG == 4.0: - modG = [2.5,3.0,3.5,4.0,4.5,5.0] - - if maxZ == -2.0: - modZ = [-2.5,-2.0,-1.5,-1.0] - if maxZ == -1.0: - modZ = [-1.5,-1.0,-0.5,0.0] - if maxZ == 0.0: - modZ = [-0.5,0.0,0.2,0.5] - - MOD = [] - - for i in modZ: - for ig in modG: - for it in modT: - MK = 'R_'+str(rot)+'_'+get_name(it,ig,i) - if os.access(model_path1+MK,os.F_OK): - MOD.append(MK) - - #maxT,maxG,maxZ,maxCor = 5500,4.0,0,0 - maxCor = 0.0 - minCHIS = 10000000000000000. - for m in MOD: - calculated.append(m) - hd = pyfits.getheader(model_path1+m) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path1+m) - FF = sc[It] - mwa = wat*(1+velo2/lux) - - #NCCF = corr(L,F,mwa,FF,ons) - CHIS = get_chis(L,F,SNR,mwa,FF,ons) - NCCF = 1./CHIS - - vecT.append(T) - vecG.append(G) - vecZ.append(Z) - vecR.append(rot) - vecCF.append(NCCF) - - if NCCF > maxCor: - maxCor = NCCF - maxT = T - maxG = G - maxZ = Z - - elif NCCF < 0: - print "Problem with spectrum!!! -> Negative value of CCF." - maxG = 4.5 - maxT = 5500 - maxZ = 0 - maxvs = 0 - - #print "maxfino",maxT,maxG,maxZ,maxCor - #print "maxfino",minT,minG,minZ,maxCor - - TEI = maxT - LGI = maxG - MEI = maxZ - - ultrot = rot - - if maxCor > maximo: - maximo = maxCor - Tfin,Gfin,Zfin,rotfin,velfin = maxT,maxG,maxZ,rot,velo2 - #Tf,Gf,Zf = intert,interg,maZ - - late = False - rotss.append(rot) - - - itera = itera+1 - if maximo == 0: - Tfin,Gfin,Zfin,rotfin,velfin = 5500, 4.5,0,0,velo2 - #print 'Pars fase 0:', Tfin, Gfin, Zfin,rotfin,maximo - mejor = False - if rotfin == 0.0: - nrot = [0.0,2.5] - elif rotfin == 2.5: - nrot = [0.0,2.5,5.0] - elif rotfin == 5.0: - nrot = [2.5,5.0,7.5] - elif rotfin == 7.5: - nrot = [5.0,7.5,10.0] - else: - nrot = [rotfin-5.0,rotfin,rotfin+5.0] - - if Tfin == 3500: - nT = [3500,3750,4000] - elif Tfin == 7000: - nT = [6500,6750,7000] - else: - nT = [Tfin-250,Tfin,Tfin+250] - - if Gfin == 0.0: - nG = [0.0,0.5,1.0] - elif Gfin == 0.5: - nG = [0.0,0.5,1.0] - elif Gfin == 4.5: - nG = [4.0,4.5,5.0] - elif Gfin == 5.0: - nG = [4.0,4.5,5.0] - else: - nG = [Gfin-0.5,Gfin,Gfin+0.5] - - if Zfin == -2.5: - nZ = [-2.5,-2.0,-1.5] - elif Zfin == 0.5: - nZ = [0.0,0.2,0.5] - elif Zfin == 0.2: - nZ = [0.0,0.2,0.5] - elif Zfin == 0.0: - nZ = [-0.5,0.0,0.2,0.5] - else: - nZ = [Zfin-0.5,Zfin,Zfin+0.5] - - for v in nrot: - model_path2 = model_path+'vsini_'+str(v)+'/' - names = [] - calc = numpy.array(calculated) - - for t in nT: - for g in nG: - for z in nZ: - nam = 'R_'+str(v)+'_'+get_name(t,g,z) - I = numpy.where(calc == nam)[0] - if len(I)==0 and os.access(model_path2+nam,os.F_OK): - names.append(nam) - - for fits in names: - calculated.append(fits) - hd = pyfits.getheader(model_path2+fits) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path2+fits) - FF = sc[It] - mwa = wat*(1+velfin/lux) - - NCCF = corr(L,F,mwa,FF,ons) - CHIS = get_chis(L,F,SNR,mwa,FF,ons) - #print T,G,Z,vs,CHIS - vecT.append(T) - vecG.append(G) - vecZ.append(Z) - vecCF.append(NCCF) - vecR.append(v) - #print T,G,Z,NCCF - - if NCCF > maximo: - mejor = True - maximo = NCCF - Tfin = T - Gfin = G - Zfin = Z - rotfin = v - - #print Tfin,Gfin,Zfin,rotfin,maximo - maximCCF = maximo - - total_inter = 1 - if total_inter == 0: - deltaV = 0.5 - deltaG = 0.05 - deltaT = 50.0 - deltaZ = 0.1 - - vCF = numpy.array(vecCF) - vvT = numpy.array(vecT) - vvG = numpy.array(vecG) - vvZ = numpy.array(vecZ) - vvV = numpy.array(vecR) - - ejeT = numpy.arange(numpy.min(nT),numpy.max(nT)+deltaT,deltaT) - ejeV = numpy.arange(numpy.min(nrot),numpy.max(nrot)+deltaV,deltaV) - ejeG = numpy.arange(numpy.min(nG),numpy.max(nG)+deltaG,deltaG) - ejeZ = numpy.arange(numpy.min(nZ),numpy.max(nZ)+deltaZ,deltaZ) - - lejeV = len(ejeV) - lejeG = len(ejeG) - lejeT = len(ejeT) - lejeZ = len(ejeZ) - - matCCF = numpy.zeros([lejeT,lejeG,lejeZ,lejeV],float) - - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for z in nZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - for g in nG: - posG = int(round((g-ejeG[0])/deltaG)) - I = numpy.where((vvV == v) & (vvG ==g) & (vvZ == z))[0] - if len(I) > 0: - vTt,vCFt = orden(vvT[I],vCF[I]) - - if len(I)>3: - tck = interpolate.splrep(vTt,vCFt,k=3,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - else: - ynew = numpy.zeros(len(ejeT),float)+vCFt[0] - - matCCF[:,posG,posZ,posV]=ynew - - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for z in nZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - y1 = matCCF[posT,:,posZ,posV] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeG[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - else: - ynew = numpy.zeros(len(ejeG),float)+y1b[0] - matCCF[posT,:,posZ,posV]=ynew - for v in nrot: - posV = int(round((v-ejeV[0])/deltaV)) - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - for g in ejeG: - posG = int(round((g-ejeG[0])/deltaG)) - y1 = matCCF[posT,posG,:,posV] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeZ[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeZ,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeZ,tck,der=0) - else: - ynew = numpy.zeros(len(ejeZ),float)+y1b[0] - matCCF[posT,posG,:,posV]=ynew - - for t in ejeT: - posT = int(round((t-ejeT[0])/deltaT)) - for g in ejeG: - posG = int(round((g-ejeG[0])/deltaG)) - for z in ejeZ: - posZ = int(round((z-ejeZ[0])/deltaZ)) - y1 = matCCF[posT,posG,posZ,:] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeV[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeV,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeV,tck,der=0) - else: - ynew = numpy.zeros(len(ejeV),float)+y1b[0] - matCCF[posT,posG,posZ,:]=ynew - - I = numpy.where(matCCF == numpy.max(matCCF)) - - intert = ejeT[I[0]] - interg = ejeG[I[1]] - interz = ejeZ[I[2]] - interv = ejeV[I[3]] - maximCCF = numpy.max(CCF) - #print 'interp',intert,interg,interz,interv - - elif total_inter == 1: - if Tfin == 3500: - nT = [3500,3750,4000,4250] - elif Tfin == 3750: - nT = [3500,3750,4000,4250] - elif Tfin == 7000: - nT = [6250,6500,6750,7000] - elif Tfin == 6750: - nT = [6250,6500,6750,7000] - else: - nT = [Tfin-500,Tfin-250,Tfin,Tfin+250,Tfin+500] - - if Gfin == 0.0: - nG = [0.0,0.5,1.0,1.5] - elif Gfin == 0.5: - nG = [0.0,0.5,1.0,1.5] - elif Gfin == 4.5: - nG = [3.5,4.0,4.5,5.0] - elif Gfin == 5.0: - nG = [3.5,4.0,4.5,5.0] - else: - nG = [Gfin-1.0,Gfin-0.5,Gfin,Gfin+0.5,Gfin+1.0] - calc = numpy.array(calculated) - model_path2 = model_path+'vsini_'+str(rotfin)+'/' - names = [] - - for t in nT: - for g in nG: - nam = 'R_'+str(rotfin)+'_'+get_name(t,g,Zfin) - I = numpy.where(calc == nam)[0] - - if len(I) == 0 and os.access(model_path2+nam,os.F_OK): - names.append(nam) - - for fits in names: - calculated.append(fits) - hd = pyfits.getheader(model_path2+fits) - T = hd['TEFF'] - G = hd['LOG_G'] - Z = hd['FEH'] - - sc = pyfits.getdata(model_path2+fits) - FF = sc[It] - mwa = wat*(1+velfin/lux) - - NCCF = corr(L,F,mwa,FF,ons) - CHIS = get_chis(L,F,SNR,mwa,FF,ons) - #print T,G,Z,vs,CHIS - vecZ.append(Z) - vecT.append(T) - vecG.append(G) - vecR.append(rotfin) - vecCF.append(NCCF) - - - VZ = numpy.array(vecZ) - VR = numpy.array(vecR) - VT = numpy.array(vecT) - VG = numpy.array(vecG) - VF = numpy.array(vecCF) - - I = numpy.where((VZ == Zfin) & (VR == rotfin))[0] - VT2 = VT[I] - VG2 = VG[I] - VF2 = VF[I] - deltaT = 50.0 - deltaG = 0.05 - - ejeT = numpy.arange(numpy.min(numpy.array(nT)),numpy.max(numpy.array(nT))+deltaT,deltaT) - ejeG = numpy.arange(numpy.min(numpy.array(nG)),numpy.max(numpy.array(nG))+deltaG,deltaG) - - lejeT = len(ejeT) - lejeG = len(ejeG) - - matCCF = numpy.zeros([lejeT,lejeG],float) - - for g in nG: - pos = int(round((g-ejeG[0])/deltaG)) - I = numpy.where(VG2 == g)[0] - if len(I) > 0: - - vTt,vCFt = orden(VT2[I],VF2[I]) - #print vTt,vCFt - if len(I)>3: - tck = interpolate.splrep(vTt,vCFt,k=3,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeT,tck,der=0) - else: - ynew = numpy.zeros(len(ejeT),float)+vCFt[0] - - matCCF[:,pos]=ynew - - for t in ejeT: - pos1 = int(round((t-ejeT[0])/deltaT)) - y1 = matCCF[pos1,:] - I = numpy.where(y1 != 0.0)[0] - y1b = y1[I] - x1b = ejeG[I] - if len(I) > 0: - if len(I)>3: - tck = interpolate.splrep(x1b,y1b,k=3,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - elif len(I) > 1: - tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) - ynew = interpolate.splev(ejeG,tck,der=0) - else: - ynew = numpy.zeros(len(ejeG),float)+y1b[0] - matCCF[pos1,:]=ynew - - I = numpy.where(matCCF == numpy.max(matCCF)) - - intert = ejeT[I[0]][0] - interg = ejeG[I[1]][0] - interz = Zfin - interv = rotfin - #print intert,interg - maximCCF = numpy.max(matCCF) - - else: - intert,interg,interz,interv = Tfin,Gfin,Zfin,rotfin - #print 'HI' - if doplot: - nam = 'R_'+str(rotfin)+'_'+get_name(Tfin,Gfin,Zfin) - hd = pyfits.getheader(model_path2+nam) - sc = pyfits.getdata(model_path2+nam) - PWAV = ToVacuum(numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1']) - PWAV = PWAV*(1+velfin/lux) - for i in range(L1.shape[0]): - I = numpy.where((PWAV > L1[i,0]) & (PWAV < L1[i,-1]))[0] - #print L1[i] - #print F1[i] - #print i - f = figure() - ylim(0.,1.01) - plot(L1[i],F1[i]) - plot(PWAV[I],sc[I]) - xlabel('wavelenth [A]') - ylabel('Continuum Normalized Flux') - savefig(plot_dir + plot_name + '_' + str(int(L1[i,0])) + '_' + str(int(L1[i,-1])) + '.pdf', format='pdf') - - - return [intert, interg, interz, interv, velfin,maximCCF] \ No newline at end of file + """ + This function finds an aproximation to the stellar parameters (Teff, log(g), [Fe/H]) + of the input echelle spectrum using a CCF with model spectra. This code also is + constructed to find the radial velocity of the star and v*sin(i). + """ + + lux = 299792.458 + + L1,F1,SNR1 = spec[0,:,:], spec[5,:,:], spec[8,:,:] + + #width_path = '/home/rabrahm/Desktop/corr2/' + #slines_path = '/home/rabrahm/Desktop/corr2/' + width_path = '../utils/Correlation/' + slines_path = '../utils/Correlation/' + + SLi,SLf = numpy.loadtxt(slines_path+'lines2.dat',dtype=None,unpack=True) + + SLi = ToVacuum(SLi) + SLf = ToVacuum(SLf) + + AT,AG,AZ,A000,A025,A050,A075,A100,A150,A200,A250,A300,A350,A400,A450,A500 =\ + numpy.loadtxt(width_path+'anchos50000.dat',dtype=None,unpack=True) + + vsini = [0.0,2.5,5.0,7.5,10.0,15.0,20.0,25.0,30.0,35.0,40.0,45.0,50.0] + + or01 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 6300.0)[0] + if len(I) > 0: + or01 = i + break + + or02 = L1.shape[0]-1 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 4500.0)[0] + if len(I) > 0: + or02 = i + break + + or03 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 6250.0)[0] + if len(I) > 0: + or03 = i + break + + or04 = L1.shape[0]-1 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 5500.0)[0] + if len(I) > 0: + or04 = i + break + + or05 = 0 + for i in range(L1.shape[0]): + I = numpy.where(L1[i] < 5190.0)[0] + W = numpy.where(L1[i] < 5170.0)[0] + if len(I) > 0 and len(W) > 0: + or05 = i + break + #print or01,or02,or03,or04,or05 + guess = [1.0,1.0,1.0] + + L = L1[or01:or02] + F = F1[or01:or02] + SNR = SNR1[or01:or02] + Lm = L1[or03:or04] + Fm = F1[or03:or04] + SNRm = SNR1[or03:or04] + Lg = L1[or05] + Fg = F1[or05] + SNRg = SNR1[or05] + + bad_lines = [[6860,6900],[6550,6580],[6270,6320],[4850,4880]] + ons = bad_orders(L,bad_lines) + + modi = 'R_0.0_5000_30_p00p00.ms.fits' + #print 'Radial velocity calculation via CCF with: '+modi + + sci = pyfits.getdata(model_path+'vsini_0.0/'+modi) + hdi = pyfits.getheader(model_path+'vsini_0.0/'+modi) + + wam1 = ToVacuum(numpy.arange(len(sci))*hdi['CD1_1']+hdi['CRVAL1']) + + Im = numpy.where((wam1 > 5400.0) & (wam1 < 6350.0))[0] + wam = wam1[Im] + flm = sci[Im] + + Ig = numpy.where((wam1 > 5100.0) & (wam1 < 5250.0))[0] + wag = wam1[Ig] + + It = numpy.where((wam1 > 4000.0) & (wam1 < 7500.0))[0] + wat = wam1[It] + + for i in range(Lm.shape[0]): + I = numpy.where((Fm[i] != 0.0) & (Fm[i] < 2.0))[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I] + MFs = flm[I] + + vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200.0,200.0) + + if i == 0: + ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = Ls[-1]-Ls[0] + else: + ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + + + ccf = ccf/nor + vv = numpy.array(vv) + + B = 0.5*(ccf[0]+ccf[-1]) + A = numpy.max(ccf)-B + med = vv[numpy.where(ccf == numpy.max(ccf))[0]] + sig = 20.0 + guess1 = [B,A,med,sig] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) + velo = ajustep[0][2] + + #print 'The initial radial velocity is: '+str(velo)+' km/s' + + #print 'Determining parameters of the initial model' + + vecti = [3500,4000,4500,5000,5500,6500] + vecgi = [0.0,1.5, 3.0,4.5] + #veczi = [-2.0,-1.0,0.0] + veczi = [-1.0,0.0] + ccmax = 0 + names = [] + cc = 0 + nor = 0 + for g in vecgi: + for z in veczi: + for t in vecti: + nam = get_name(t,g,z) + names.append(nam) + namemax = get_name(5500,4.5,0.0) + TEI = 5500 + MEI = 0.0 + LGI = 4.5 + for nam in names: + if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): + mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) + flm = mod[Im] + cc = 0.0 + nor = 0.0 + for o in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I]*(1+velo/lux) + MFs = flm[I] + + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + cc = cc + integrate.simps(NMFs[1:-1]*Fs[1:-1],Ls[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]))*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + cc = cc/nor + + if cc >=ccmax: + ccmax = cc + namemax = nam + + mod = pyfits.getheader(model_path+'vsini_0.0/R_0.0_'+namemax) + TEI = mod['TEFF'] + + #print 'Teff (initial) = '+str(TEI)+' K' + + + + if TEI <= 4000: + rot = 5.0 + LGI = 3.0 + MTI = 0.0 + late = True + velo2 = velo + + else: + late = False + t = TEI + vecgi = [1.0,2.0,3.0,4.0] + #veczi = [-2.0,-1.0,0.0] + veczi = [-1.0,0.0] + dif = 1000.0 + + for z in veczi: + vals = [] + di = 1000 + meg = 3.0 + for g in vecgi: + nam = get_name(t,g,z) + if ( (os.access(model_path+'vsini_0.0/R_0.0_'+nam,os.F_OK) == True)): + mod = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+nam) + flm = mod[Im] + flm = el_stl(wam,flm,SLi,SLf) + intfl = 0.0 + intflm = 0.0 + di = 1000 + for o in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + MLs = wam[I]*(1+velo/lux) + MFs = flm[I] + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + intfl = intfl + integrate.simps(Fs[1:-1]*Fs[1:-1],Ls[1:-1]) + intflm = intflm + integrate.simps(NMFs[1:-1]*NMFs[1:-1],Ls[1:-1]) + intfl = math.sqrt(intfl) + intflm = math.sqrt(intflm) + dif1 = math.sqrt((intflm-intfl)**2) + vals.append(dif1) + if dif1 < di: + di = dif1 + meg = g + dif2 = numpy.mean(vals) + #print z,dif2 + if dif2 Ls[0]-5.0) & (wag < Ls[-1]+5.0))[0] + MLs = wag[I]*(1+velo/lux) + MFs = mflg[I] + tck = interpolate.splrep(MLs,MFs,k=3,s=0) + NMFs = interpolate.splev(Ls,tck,der=0) + + cc2 = integrate.simps(Fs[1:-1]*NMFs[1:-1])/math.sqrt(integrate.simps(Fs[1:-1]*Fs[1:-1])*integrate.simps(NMFs[1:-1]*NMFs[1:-1])) + #print g,cc2 + if cc2 > cfi2: + cfi2 = cc2 + LGI = g + + #print 'Log(g) (initial) = '+str(LGI) + + itera = 0 + maximo = 0 + calculated = [] + rotss = [] + vecR = [] + vecT = [] + vecG = [] + + vecZ = [] + vecCF = [] + while itera < 4: + + if late==False: + + MOG = get_name(TEI,LGI,MEI) + sc = pyfits.getdata(model_path+'vsini_0.0/R_0.0_'+MOG) + + #print '-Calculating radial shift and v*sin(i) with model: '+MOG + + flm = sc[Im] + ies = [] + flm = el_stl(wam,flm,SLi,SLf) + for i in range(Lm.shape[0]): + I = numpy.where(Fm[i] != 0.0)[0] + Ls = Lm[i][I] + Fs = Fm[i][I] + Fs = el_stl(Ls,Fs,SLi*(1+velo/lux),SLf*(1+velo/lux)) + I = numpy.where((wam > Ls[0]-5.0) & (wam < Ls[-1]+5.0))[0] + ies.append(I) + MLs = wam[I] + MFs = flm[I] + vv,cc = vels.CCF(MLs,MFs,Ls,Fs,-200,200) + if i == 0: + ccf = numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = Ls[-1]-Ls[0] + else: + ccf = ccf + numpy.array(cc)*(Ls[-1]-Ls[0]) + nor = nor + (Ls[-1]-Ls[0]) + nor2=1. + ccf = ccf/nor + vv = numpy.array(vv) + B = 0.5*(ccf[0]+ccf[-1]) + A = numpy.max(ccf)-B + med = vv[numpy.where(ccf == numpy.max(ccf))[0]] + sig = 20.0 + guess1 = [B,A,med,sig] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(ccf,vv)) + velo2 = ajustep[0][2] + sig2 = ajustep[0][3] + sig2 = math.sqrt(sig2*sig2) + #plt.plot(vv,ccf,vv,gauss1(ajustep[0],vv)) + #plt.show() + #print 'radial velocity = '+str(velo2)+' km/s' + #print 'Sigma = '+str(sig2)+' km/s' + """ + + vi = 0.0 + difsigmin = 1000 + while vi <= 20.0: + ai = 0 + if ( (os.access(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG,os.F_OK) == True)): + modt = pyfits.getdata(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG) + flm2 = modt[Im] + flm2 = el_stl(wam,flm2,SLi,SLf) + for I in ies: + Fs = flm[I] + Ls = wam[I] + Fs = el_stl(Ls,Fs,SLi,SLf) + MFs = flm2[I] + vv2,cc2 = vels.CCF(Ls,MFs,Ls,Fs,-200,200) + if ai == 0: + ccf2 = numpy.array(cc2)*(Ls[-1]-Ls[0]) + nor2 = Ls[-1]-Ls[0] + else: + ccf2 = ccf2 + numpy.array(cc2)*(Ls[-1]-Ls[0]) + nor2 = nor2 + (Ls[-1]-Ls[0]) + ai += 1 + cc2 = ccf2/nor2 + vv2 = numpy.array(vv2) + B3 = 0.5*(cc2[0]+cc2[-1]) + A3 = numpy.max(cc2)-B3 + med3 = 0.0 + sig3 = 20.0 + guess1 = [B3,A3,med3,sig3] + ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2))) + cte3 = ajustep[0][0] + no3 = ajustep[0][1] + med3 = ajustep[0][2] + sig3 = ajustep[0][3] + + #plt.plot(vv2,cc2) + + print vi,sig3 + difsig = math.sqrt((sig2-sig3)**2) + if difsig < difsigmin: + difsigmin = difsig + rot = vi + ai +=1 + + if vi <= 7.5: + vi = vi+2.5 + elif vi < 50.0: + vi = vi+5.0 + else: + break + + #plt.show() + """ + if sig2 >= vsini[0]: + I = numpy.where((AT == TEI) & (AG == LGI) & (AZ == MEI))[0][0] + anchos = numpy.array([A000[I],A025[I],A050[I],A075[I],A100[I],A150[I],A200[I],A250[I],A300[I],A350[I],A400[I],A450[I],A500[I]]) + kis = 0 + while kis < len(anchos)-1: + if anchos[kis]>anchos[kis+1]: + portemp = anchos[kis] + anchos[kis] = anchos[kis+1] + anchos[kis+1] = portemp + kis+=1 + tck = interpolate.splrep(anchos,numpy.array(vsini),k=3,s=0) + calrot = interpolate.splev(sig2,tck,der=0) + difs = (numpy.array(vsini) - calrot)**2 + AI = numpy.where(difs == numpy.min(difs))[0] + rot = vsini[AI] + else: + rot = vsini[0] + calrot = 0.0 + + #rot = 2.5 + #print 'v*sin(i) = '+str(calrot)+' km/s' + + RI = numpy.where(numpy.array(rotss) == rot)[0] + + if len(RI) > 0: + break + + else: + + model_path1 = model_path+"vsini_"+str(rot)+"/" + + """ conociendo la velocidad radial realizo una busqueda gruesa de parametros estelares calculando el maximo de la CCF""" + #print "-Searching the optimal stellar model" + + #maxT,maxG,maxZ,maxCor,maxvs = 5500,4.0,0.0,0.0,0.0 + maxCor = 0.0 + minCHIS = 100000000000000000000. + if TEI >= 3500 and TEI <=4250: + modT = [3500,4000,4500,5000] + else: + modT = [4000,4500,5000,5500,6000,6500,7000] + + modG = [1.0,2.5,4.0] + #modZ = [-2.0,-1.0,0.0] + modZ = [-1.0,0.0] + MOD = [] + + i=0 + while i < len(modZ): + ig = 0 + while ig < len(modG): + it = 0 + while it < len(modT): + MK = 'R_'+str(rot)+'_'+get_name(modT[it],modG[ig],modZ[i]) + if os.access(model_path1+MK,os.F_OK): + MOD.append(MK) + it = it+1 + ig =ig+1 + i = i+1 + + for m in MOD: + + hd = pyfits.getheader(model_path1+m) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + vs = hd['VSINI'] + + sc = pyfits.getdata(model_path1+m) + FF = sc[It] + mwa = wat*(1+velo2/lux) + + #NCCF = corr(L,F,mwa,FF,ons) + CHIS = get_chis(L,F,SNR,mwa,FF,ons) + + NCCF = 1./CHIS + if NCCF > maxCor: + maxCor = NCCF + maxG = G + maxT = T + maxZ = Z + maxvs = vs + elif NCCF<0: + #print "Problem with spectrum!!! -> Negative value of CCF." + maxG = 4.5 + maxT = 5500 + maxZ = 0 + maxvs = 0 + + + + #print 'maxgrueso',maxT,maxG,maxZ,maxvs + + """ A partir de los parametros encontrados en la busqueda gruesa procedo a constrenir los limites de la busqueda fina""" + + + #Tfin,Gfin,Zfin,rotfin,velfin = 5500,4.0,0.0,0.0,0.0 + """ Ahora se buscan los par'ametros estelares optimos mediante una exploracion fina""" + + if maxT == 3500 or maxT == 4000: + modT = [3500,3750,4000,4250,4500] + + elif maxT == 7000 or maxT == 6500: + modT = [6000,6250,6500,6750,7000] + else: + modT = [maxT-750,maxT-500,maxT-250,maxT,maxT+250,maxT+500,maxT+750] + + if maxG == 1.0: + modG = [0.0,0.5,1.0,1.5,2.0,2.5] + if maxG == 2.5: + modG = [1.0,1.5,2.0,2.5,3.0,3.5,4.0] + if maxG == 4.0: + modG = [2.5,3.0,3.5,4.0,4.5,5.0] + + if maxZ == -2.0: + modZ = [-2.5,-2.0,-1.5,-1.0] + if maxZ == -1.0: + modZ = [-1.5,-1.0,-0.5,0.0] + if maxZ == 0.0: + modZ = [-0.5,0.0,0.2,0.5] + + MOD = [] + + for i in modZ: + for ig in modG: + for it in modT: + MK = 'R_'+str(rot)+'_'+get_name(it,ig,i) + if os.access(model_path1+MK,os.F_OK): + MOD.append(MK) + + #maxT,maxG,maxZ,maxCor = 5500,4.0,0,0 + maxCor = 0.0 + minCHIS = 10000000000000000. + for m in MOD: + calculated.append(m) + hd = pyfits.getheader(model_path1+m) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path1+m) + FF = sc[It] + mwa = wat*(1+velo2/lux) + + #NCCF = corr(L,F,mwa,FF,ons) + CHIS = get_chis(L,F,SNR,mwa,FF,ons) + NCCF = 1./CHIS + + vecT.append(T) + vecG.append(G) + vecZ.append(Z) + vecR.append(rot) + vecCF.append(NCCF) + + if NCCF > maxCor: + maxCor = NCCF + maxT = T + maxG = G + maxZ = Z + + elif NCCF < 0: + print("Problem with spectrum!!! -> Negative value of CCF.") + maxG = 4.5 + maxT = 5500 + maxZ = 0 + maxvs = 0 + + #print "maxfino",maxT,maxG,maxZ,maxCor + #print "maxfino",minT,minG,minZ,maxCor + + TEI = maxT + LGI = maxG + MEI = maxZ + + ultrot = rot + + if maxCor > maximo: + maximo = maxCor + Tfin,Gfin,Zfin,rotfin,velfin = maxT,maxG,maxZ,rot,velo2 + #Tf,Gf,Zf = intert,interg,maZ + + late = False + rotss.append(rot) + + + itera = itera+1 + if maximo == 0: + Tfin,Gfin,Zfin,rotfin,velfin = 5500, 4.5,0,0,velo2 + #print 'Pars fase 0:', Tfin, Gfin, Zfin,rotfin,maximo + mejor = False + if rotfin == 0.0: + nrot = [0.0,2.5] + elif rotfin == 2.5: + nrot = [0.0,2.5,5.0] + elif rotfin == 5.0: + nrot = [2.5,5.0,7.5] + elif rotfin == 7.5: + nrot = [5.0,7.5,10.0] + else: + nrot = [rotfin-5.0,rotfin,rotfin+5.0] + + if Tfin == 3500: + nT = [3500,3750,4000] + elif Tfin == 7000: + nT = [6500,6750,7000] + else: + nT = [Tfin-250,Tfin,Tfin+250] + + if Gfin == 0.0: + nG = [0.0,0.5,1.0] + elif Gfin == 0.5: + nG = [0.0,0.5,1.0] + elif Gfin == 4.5: + nG = [4.0,4.5,5.0] + elif Gfin == 5.0: + nG = [4.0,4.5,5.0] + else: + nG = [Gfin-0.5,Gfin,Gfin+0.5] + + if Zfin == -2.5: + nZ = [-2.5,-2.0,-1.5] + elif Zfin == 0.5: + nZ = [0.0,0.2,0.5] + elif Zfin == 0.2: + nZ = [0.0,0.2,0.5] + elif Zfin == 0.0: + nZ = [-0.5,0.0,0.2,0.5] + else: + nZ = [Zfin-0.5,Zfin,Zfin+0.5] + + for v in nrot: + model_path2 = model_path+'vsini_'+str(v)+'/' + names = [] + calc = numpy.array(calculated) + + for t in nT: + for g in nG: + for z in nZ: + nam = 'R_'+str(v)+'_'+get_name(t,g,z) + I = numpy.where(calc == nam)[0] + if len(I)==0 and os.access(model_path2+nam,os.F_OK): + names.append(nam) + + for fits in names: + calculated.append(fits) + hd = pyfits.getheader(model_path2+fits) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path2+fits) + FF = sc[It] + mwa = wat*(1+velfin/lux) + + NCCF = corr(L,F,mwa,FF,ons) + CHIS = get_chis(L,F,SNR,mwa,FF,ons) + #print T,G,Z,vs,CHIS + vecT.append(T) + vecG.append(G) + vecZ.append(Z) + vecCF.append(NCCF) + vecR.append(v) + #print T,G,Z,NCCF + + if NCCF > maximo: + mejor = True + maximo = NCCF + Tfin = T + Gfin = G + Zfin = Z + rotfin = v + + #print Tfin,Gfin,Zfin,rotfin,maximo + maximCCF = maximo + + total_inter = 1 + if total_inter == 0: + deltaV = 0.5 + deltaG = 0.05 + deltaT = 50.0 + deltaZ = 0.1 + + vCF = numpy.array(vecCF) + vvT = numpy.array(vecT) + vvG = numpy.array(vecG) + vvZ = numpy.array(vecZ) + vvV = numpy.array(vecR) + + ejeT = numpy.arange(numpy.min(nT),numpy.max(nT)+deltaT,deltaT) + ejeV = numpy.arange(numpy.min(nrot),numpy.max(nrot)+deltaV,deltaV) + ejeG = numpy.arange(numpy.min(nG),numpy.max(nG)+deltaG,deltaG) + ejeZ = numpy.arange(numpy.min(nZ),numpy.max(nZ)+deltaZ,deltaZ) + + lejeV = len(ejeV) + lejeG = len(ejeG) + lejeT = len(ejeT) + lejeZ = len(ejeZ) + + matCCF = numpy.zeros([lejeT,lejeG,lejeZ,lejeV],float) + + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for z in nZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + for g in nG: + posG = int(round((g-ejeG[0])/deltaG)) + I = numpy.where((vvV == v) & (vvG ==g) & (vvZ == z))[0] + if len(I) > 0: + vTt,vCFt = orden(vvT[I],vCF[I]) + + if len(I)>3: + tck = interpolate.splrep(vTt,vCFt,k=3,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + else: + ynew = numpy.zeros(len(ejeT),float)+vCFt[0] + + matCCF[:,posG,posZ,posV]=ynew + + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for z in nZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + y1 = matCCF[posT,:,posZ,posV] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeG[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + else: + ynew = numpy.zeros(len(ejeG),float)+y1b[0] + matCCF[posT,:,posZ,posV]=ynew + for v in nrot: + posV = int(round((v-ejeV[0])/deltaV)) + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + for g in ejeG: + posG = int(round((g-ejeG[0])/deltaG)) + y1 = matCCF[posT,posG,:,posV] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeZ[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeZ,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeZ,tck,der=0) + else: + ynew = numpy.zeros(len(ejeZ),float)+y1b[0] + matCCF[posT,posG,:,posV]=ynew + + for t in ejeT: + posT = int(round((t-ejeT[0])/deltaT)) + for g in ejeG: + posG = int(round((g-ejeG[0])/deltaG)) + for z in ejeZ: + posZ = int(round((z-ejeZ[0])/deltaZ)) + y1 = matCCF[posT,posG,posZ,:] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeV[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeV,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeV,tck,der=0) + else: + ynew = numpy.zeros(len(ejeV),float)+y1b[0] + matCCF[posT,posG,posZ,:]=ynew + + I = numpy.where(matCCF == numpy.max(matCCF)) + + intert = ejeT[I[0]] + interg = ejeG[I[1]] + interz = ejeZ[I[2]] + interv = ejeV[I[3]] + maximCCF = numpy.max(CCF) + #print 'interp',intert,interg,interz,interv + + elif total_inter == 1: + if Tfin == 3500: + nT = [3500,3750,4000,4250] + elif Tfin == 3750: + nT = [3500,3750,4000,4250] + elif Tfin == 7000: + nT = [6250,6500,6750,7000] + elif Tfin == 6750: + nT = [6250,6500,6750,7000] + else: + nT = [Tfin-500,Tfin-250,Tfin,Tfin+250,Tfin+500] + + if Gfin == 0.0: + nG = [0.0,0.5,1.0,1.5] + elif Gfin == 0.5: + nG = [0.0,0.5,1.0,1.5] + elif Gfin == 4.5: + nG = [3.5,4.0,4.5,5.0] + elif Gfin == 5.0: + nG = [3.5,4.0,4.5,5.0] + else: + nG = [Gfin-1.0,Gfin-0.5,Gfin,Gfin+0.5,Gfin+1.0] + calc = numpy.array(calculated) + model_path2 = model_path+'vsini_'+str(rotfin)+'/' + names = [] + + for t in nT: + for g in nG: + nam = 'R_'+str(rotfin)+'_'+get_name(t,g,Zfin) + I = numpy.where(calc == nam)[0] + + if len(I) == 0 and os.access(model_path2+nam,os.F_OK): + names.append(nam) + + for fits in names: + calculated.append(fits) + hd = pyfits.getheader(model_path2+fits) + T = hd['TEFF'] + G = hd['LOG_G'] + Z = hd['FEH'] + + sc = pyfits.getdata(model_path2+fits) + FF = sc[It] + mwa = wat*(1+velfin/lux) + + NCCF = corr(L,F,mwa,FF,ons) + CHIS = get_chis(L,F,SNR,mwa,FF,ons) + #print T,G,Z,vs,CHIS + vecZ.append(Z) + vecT.append(T) + vecG.append(G) + vecR.append(rotfin) + vecCF.append(NCCF) + + + VZ = numpy.array(vecZ) + VR = numpy.array(vecR) + VT = numpy.array(vecT) + VG = numpy.array(vecG) + VF = numpy.array(vecCF) + + I = numpy.where((VZ == Zfin) & (VR == rotfin))[0] + VT2 = VT[I] + VG2 = VG[I] + VF2 = VF[I] + deltaT = 50.0 + deltaG = 0.05 + + ejeT = numpy.arange(numpy.min(numpy.array(nT)),numpy.max(numpy.array(nT))+deltaT,deltaT) + ejeG = numpy.arange(numpy.min(numpy.array(nG)),numpy.max(numpy.array(nG))+deltaG,deltaG) + + lejeT = len(ejeT) + lejeG = len(ejeG) + + matCCF = numpy.zeros([lejeT,lejeG],float) + + for g in nG: + pos = int(round((g-ejeG[0])/deltaG)) + I = numpy.where(VG2 == g)[0] + if len(I) > 0: + + vTt,vCFt = orden(VT2[I],VF2[I]) + #print vTt,vCFt + if len(I)>3: + tck = interpolate.splrep(vTt,vCFt,k=3,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(vTt,vCFt,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeT,tck,der=0) + else: + ynew = numpy.zeros(len(ejeT),float)+vCFt[0] + + matCCF[:,pos]=ynew + + for t in ejeT: + pos1 = int(round((t-ejeT[0])/deltaT)) + y1 = matCCF[pos1,:] + I = numpy.where(y1 != 0.0)[0] + y1b = y1[I] + x1b = ejeG[I] + if len(I) > 0: + if len(I)>3: + tck = interpolate.splrep(x1b,y1b,k=3,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + elif len(I) > 1: + tck = interpolate.splrep(x1b,y1b,k=len(I)-1,s=0) + ynew = interpolate.splev(ejeG,tck,der=0) + else: + ynew = numpy.zeros(len(ejeG),float)+y1b[0] + matCCF[pos1,:]=ynew + + I = numpy.where(matCCF == numpy.max(matCCF)) + + intert = ejeT[I[0]][0] + interg = ejeG[I[1]][0] + interz = Zfin + interv = rotfin + #print intert,interg + maximCCF = numpy.max(matCCF) + + else: + intert,interg,interz,interv = Tfin,Gfin,Zfin,rotfin + #print 'HI' + if doplot: + nam = 'R_'+str(rotfin)+'_'+get_name(Tfin,Gfin,Zfin) + hd = pyfits.getheader(model_path2+nam) + sc = pyfits.getdata(model_path2+nam) + PWAV = ToVacuum(numpy.arange(len(sc))*hd['CD1_1']+hd['CRVAL1']) + PWAV = PWAV*(1+velfin/lux) + for i in range(L1.shape[0]): + I = numpy.where((PWAV > L1[i,0]) & (PWAV < L1[i,-1]))[0] + #print L1[i] + #print F1[i] + #print i + f = figure() + ylim(0.,1.01) + plot(L1[i],F1[i]) + plot(PWAV[I],sc[I]) + xlabel('wavelenth [A]') + ylabel('Continuum Normalized Flux') + savefig(plot_dir + plot_name + '_' + str(int(L1[i,0])) + '_' + str(int(L1[i,-1])) + '.pdf', format='pdf') + + + return [intert, interg, interz, interv, velfin,maximCCF] diff --git a/utils/Correlation/test.py b/utils/Correlation/test.py index d596b40..f8c0763 100644 --- a/utils/Correlation/test.py +++ b/utils/Correlation/test.py @@ -1,3 +1,4 @@ +from __future__ import print_function #!/usr/bin/python import numpy @@ -17,15 +18,15 @@ Vv = numpy.zeros(cant,float) """print spectra""" m=5 -print spectra +print(spectra) while m= ini) & (L1 <= fin))[0] - II = numpy.where((L2p >= ini) & (L2p <= fin))[0] - if len(I)==0 or len(II)==0: - print 'Problem: no wavelenght intersection' - - wav = numpy.arange(ini,fin,delta) - tck1 = interpolate.splrep(L1,F1,k=3,s=0) - tck2 = interpolate.splrep(L2p,F2,k=3,s=0) - F1s = interpolate.splev(wav,tck1,der=0) - F2s = interpolate.splev(wav,tck2,der=0) - - CF.append(numpy.add.reduce(F1s*F2s)/numpy.sqrt(numpy.add.reduce(F1s*F1s)*numpy.add.reduce(F2s*F2s))) - vels.append(vel) - vel = vel + 1 - - return vels,CF + lux = 299792.458 + vel = vi + delta = L1[1]-L1[0] + CF = [] + vels = [] + while vel <=vf: + + L2p = L2*(1-vel/lux) + ini,fin = intercec(L1,L2p) + + I = numpy.where((L1 >= ini) & (L1 <= fin))[0] + II = numpy.where((L2p >= ini) & (L2p <= fin))[0] + if len(I)==0 or len(II)==0: + print('Problem: no wavelenght intersection') + + wav = numpy.arange(ini,fin,delta) + tck1 = interpolate.splrep(L1,F1,k=3,s=0) + tck2 = interpolate.splrep(L2p,F2,k=3,s=0) + F1s = interpolate.splev(wav,tck1,der=0) + F2s = interpolate.splev(wav,tck2,der=0) + + CF.append(numpy.add.reduce(F1s*F2s)/numpy.sqrt(numpy.add.reduce(F1s*F1s)*numpy.add.reduce(F2s*F2s))) + vels.append(vel) + vel = vel + 1 + + return vels,CF diff --git a/utils/FastRotators/calc_pars.py b/utils/FastRotators/calc_pars.py index bcec6fb..d96633c 100644 --- a/utils/FastRotators/calc_pars.py +++ b/utils/FastRotators/calc_pars.py @@ -1,3 +1,4 @@ +from __future__ import print_function import pyfits import argparse import os @@ -17,51 +18,51 @@ fixG = float(args.fixG) try: - sc = pyfits.getdata(dirin) - fits = [dirin] - dirin = dirin.split('/') - dirin = dirin[:-1] - dt = '' - for fn in dirin: - dt += fn+'/' - dirin = dt + sc = pyfits.getdata(dirin) + fits = [dirin] + dirin = dirin.split('/') + dirin = dirin[:-1] + dt = '' + for fn in dirin: + dt += fn+'/' + dirin = dt except: - fits = glob.glob(dirin + '/*fits') + fits = glob.glob(dirin + '/*fits') os.system('mkdir '+dirin+'/FR') -print '\n\n' +print('\n\n') for fit in fits: - print 'Estimation of atmospheric parameters for ' + fit + '...' + print('Estimation of atmospheric parameters for ' + fit + '...') - sc = pyfits.getdata(fit) - hd = pyfits.getheader(fit) - teff, logg, feh, vsini = spfr.get_pars_fr(sc[0],sc[5],model_patht=model_path,npools=npools,fixG=fixG) - print '\tTeff=', teff, 'K' - print '\tlog(g)=', logg, 'dex' - print '\t[Fe/H]=', feh, 'dex' - print '\tvsini=', vsini, 'km/s\n' + sc = pyfits.getdata(fit) + hd = pyfits.getheader(fit) + teff, logg, feh, vsini = spfr.get_pars_fr(sc[0],sc[5],model_patht=model_path,npools=npools,fixG=fixG) + print('\tTeff=', teff, 'K') + print('\tlog(g)=', logg, 'dex') + print('\t[Fe/H]=', feh, 'dex') + print('\tvsini=', vsini, 'km/s\n') - line_out = "%-15s %18.8f %5.2f %5.2f %5.1f %4.2f %s\n"%(hd['HIERARCH TARGET NAME'], 2400000.5 + float(hd['HIERARCH MBJD']), teff, logg, feh, vsini, fit) + line_out = "%-15s %18.8f %5.2f %5.2f %5.1f %4.2f %s\n"%(hd['HIERARCH TARGET NAME'], 2400000.5 + float(hd['HIERARCH MBJD']), teff, logg, feh, vsini, fit) - i = 0 - isin = False - if os.access(dirin+'/FR/pars_FR.txt',os.F_OK): - f = open(dirin+'/FR/pars_FR.txt','r') - lines = f.readlines() - for line in lines: - cos = line.split() - if cos[1] == line_out.split()[1]: - lines[i] = line_out - isin = True - break - i+=1 - if not isin: - lines.append(line_out) - else: - lines = [line_out] + i = 0 + isin = False + if os.access(dirin+'/FR/pars_FR.txt',os.F_OK): + f = open(dirin+'/FR/pars_FR.txt','r') + lines = f.readlines() + for line in lines: + cos = line.split() + if cos[1] == line_out.split()[1]: + lines[i] = line_out + isin = True + break + i+=1 + if not isin: + lines.append(line_out) + else: + lines = [line_out] - f = open(dirin+'/FR/pars_FR.txt','w') - for line in lines: - f.write(line) - f.close() + f = open(dirin+'/FR/pars_FR.txt','w') + for line in lines: + f.write(line) + f.close() diff --git a/utils/FastRotators/calc_rv.py b/utils/FastRotators/calc_rv.py index 311655e..2fdee2a 100644 --- a/utils/FastRotators/calc_rv.py +++ b/utils/FastRotators/calc_rv.py @@ -1,3 +1,4 @@ +from __future__ import print_function import pyfits import argparse import os @@ -22,98 +23,93 @@ vsini = float(pars.split(',')[3]) try: - sc = pyfits.getdata(dirin) - fits = [dirin] - dirin = dirin.split('/') - dirin = dirin[:-1] - dt = '' - for fn in dirin: - dt += fn+'/' - dirin = dt + sc = pyfits.getdata(dirin) + fits = [dirin] + dirin = dirin.split('/') + dirin = dirin[:-1] + dt = '' + for fn in dirin: + dt += fn+'/' + dirin = dt except: - fits = glob.glob(dirin + '/*fits') + fits = glob.glob(dirin + '/*fits') os.system('mkdir '+dirin+'/FR') for fit in fits: - print 'RV computation of file ' + fit + ' whith model:' + pars - - sc = pyfits.getdata(fit) - hd = pyfits.getheader(fit) - - I = np.where(sc[0,:,0]<5200)[0] - npix = sc.shape[2] - SNR_5130 = np.median(sc[8,I[0],int(0.5*npix)-100:int(0.5*npix)+100] ) - if SNR_5130 < 1.: - SNR_5130 = 1. - pix = (sc[0,0,0]/(sc[0,0,1]-sc[0,0,0]))/float(hd['RESOL']) - SNR_5130_R = np.around(SNR_5130*np.sqrt(pix)) - - p1gau,vels,xc_av,XCmodelgau = spfr.RVforFR(sc[0],sc[5], teff=teff, logg=logg, feh=feh, vsini=vsini, model_path=model_path,vstep=5.) - - A = 0.11081 - B = 0.0016 - D = 0.32815 - C = 0.00453 - - RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) - BSerr = D / float(np.round(SNR_5130)) + C - - RVerr = B + (1.6+0.2*p1gau[2]*0.5)*A/np.round(SNR_5130) - depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) - if depth_fact < 0.6: - depth_fact = 0.6 - - if depth_fact >= 1.: - RVerr2 = -999.000 - else: - depth_fact = (1 - 0.6) / (1 - depth_fact) - RVerr2 = RVerr * depth_fact - - RVerr2 = np.around(RVerr2,4) - BSerr = np.around(BSerr,4) - - print '\tRV = ',p1gau[1], '+/-', RVerr2, 'km/s' - - p1gau_m = p1gau - XCmodel = XCmodelgau - xc_dict = {'p1gau':p1gau, 'vels':vels, 'xc_av':xc_av,'XCmodelgau':XCmodelgau} - ccf_pdf = dirin + '/FR/' + fit.split('/')[-1][:-4] + '_XC_FR.pdf' - pkl_xc = dirin + '/FR/' + fit.split('/')[-1][:-4] + '_XC_FR.pkl' - pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) - spfr.plot_CCF_FR(xc_dict,path=ccf_pdf) - SP2 = spfr.calc_bss2(vels,xc_av,p1gau) - - print '\tBS = ',SP2, '+/-', BSerr,'km/s' - - line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f %s ceres_FR %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ - (hd['HIERARCH TARGET NAME'], 2400000.5 + float(hd['HIERARCH MBJD']), p1gau[1], RVerr2, SP2, BSerr, hd['INST'], int(hd['RESOL']), teff, logg, feh, vsini, xc_av.min(), p1gau[2],\ - hd['TEXP (s)'], SNR_5130, ccf_pdf) - - i = 0 - isin = False - if os.access(dirin+'/FR/results_FR.txt',os.F_OK): - f = open(dirin+'/FR/results_FR.txt','r') - lines = f.readlines() - for line in lines: - cos = line.split() - if cos[1] == line_out.split()[1]: - lines[i] = line_out - isin = True - break - i+=1 - if not isin: - lines.append(line_out) - else: - lines = [line_out] - - f = open(dirin+'/FR/results_FR.txt','w') - for line in lines: - f.write(line) - f.close() - - - - - + print('RV computation of file ' + fit + ' whith model:' + pars) + + sc = pyfits.getdata(fit) + hd = pyfits.getheader(fit) + + I = np.where(sc[0,:,0]<5200)[0] + npix = sc.shape[2] + SNR_5130 = np.median(sc[8,I[0],int(0.5*npix)-100:int(0.5*npix)+100] ) + if SNR_5130 < 1.: + SNR_5130 = 1. + pix = (sc[0,0,0]/(sc[0,0,1]-sc[0,0,0]))/float(hd['RESOL']) + SNR_5130_R = np.around(SNR_5130*np.sqrt(pix)) + + p1gau,vels,xc_av,XCmodelgau = spfr.RVforFR(sc[0],sc[5], teff=teff, logg=logg, feh=feh, vsini=vsini, model_path=model_path,vstep=5.) + + A = 0.11081 + B = 0.0016 + D = 0.32815 + C = 0.00453 + + RVerr = B + ( 1.6 + 0.2 * p1gau[2] ) * A / np.round(SNR_5130) + BSerr = D / float(np.round(SNR_5130)) + C + + RVerr = B + (1.6+0.2*p1gau[2]*0.5)*A/np.round(SNR_5130) + depth_fact = 1. + p1gau[0]/(p1gau[2]*np.sqrt(2*np.pi)) + if depth_fact < 0.6: + depth_fact = 0.6 + + if depth_fact >= 1.: + RVerr2 = -999.000 + else: + depth_fact = (1 - 0.6) / (1 - depth_fact) + RVerr2 = RVerr * depth_fact + + RVerr2 = np.around(RVerr2,4) + BSerr = np.around(BSerr,4) + + print('\tRV = ',p1gau[1], '+/-', RVerr2, 'km/s') + + p1gau_m = p1gau + XCmodel = XCmodelgau + xc_dict = {'p1gau':p1gau, 'vels':vels, 'xc_av':xc_av,'XCmodelgau':XCmodelgau} + ccf_pdf = dirin + '/FR/' + fit.split('/')[-1][:-4] + '_XC_FR.pdf' + pkl_xc = dirin + '/FR/' + fit.split('/')[-1][:-4] + '_XC_FR.pkl' + pickle.dump( xc_dict, open( pkl_xc, 'w' ) ) + spfr.plot_CCF_FR(xc_dict,path=ccf_pdf) + SP2 = spfr.calc_bss2(vels,xc_av,p1gau) + + print('\tBS = ',SP2, '+/-', BSerr,'km/s') + + line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f %s ceres_FR %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ + (hd['HIERARCH TARGET NAME'], 2400000.5 + float(hd['HIERARCH MBJD']), p1gau[1], RVerr2, SP2, BSerr, hd['INST'], int(hd['RESOL']), teff, logg, feh, vsini, xc_av.min(), p1gau[2],\ + hd['TEXP (s)'], SNR_5130, ccf_pdf) + + i = 0 + isin = False + if os.access(dirin+'/FR/results_FR.txt',os.F_OK): + f = open(dirin+'/FR/results_FR.txt','r') + lines = f.readlines() + for line in lines: + cos = line.split() + if cos[1] == line_out.split()[1]: + lines[i] = line_out + isin = True + break + i+=1 + if not isin: + lines.append(line_out) + else: + lines = [line_out] + + f = open(dirin+'/FR/results_FR.txt','w') + for line in lines: + f.write(line) + f.close() diff --git a/utils/FastRotators/spfr.py b/utils/FastRotators/spfr.py index 57412a0..5474c26 100644 --- a/utils/FastRotators/spfr.py +++ b/utils/FastRotators/spfr.py @@ -1,3 +1,4 @@ +from __future__ import print_function from pylab import * import pyfits from PyAstronomy import pyasl @@ -15,29 +16,29 @@ import time def download_models(webpage='http://svo2.cab.inta-csic.es/theory/models/coelho/high/data/',dest='../../data/'): - - os.system('mkdir '+dest+'/COELHO2014') - cwd = os.getcwd() - os.chdir(dest+'/COELHO2014') - tf = np.arange(6000,10001,250) - gf = np.arange(2.5,4.6,0.5) - #gf = np.array([2.5]) - zf = np.array([-1.,-0.5,0.0,0.2]) - - for t in tf: - for g in gf: - for z in zf: - modname = get_modname(t,g,z) - if z<0: - sz = 'm' - else: - sz = 'p' - sz = sz+str(float(np.absolute(z))).replace('.','')+'p00/' - os.system('wget ' + webpage+sz+modname+'.fits') - os.system('wget ' + webpage+sz+modname+'plc.fits') - os.chdir(cwd) - - return True + + os.system('mkdir '+dest+'/COELHO2014') + cwd = os.getcwd() + os.chdir(dest+'/COELHO2014') + tf = np.arange(6000,10001,250) + gf = np.arange(2.5,4.6,0.5) + #gf = np.array([2.5]) + zf = np.array([-1.,-0.5,0.0,0.2]) + + for t in tf: + for g in gf: + for z in zf: + modname = get_modname(t,g,z) + if z<0: + sz = 'm' + else: + sz = 'p' + sz = sz+str(float(np.absolute(z))).replace('.','')+'p00/' + os.system('wget ' + webpage+sz+modname+'.fits') + os.system('wget ' + webpage+sz+modname+'plc.fits') + os.chdir(cwd) + + return True def n_Edlen(l): sigma = 1e4 / l @@ -64,684 +65,681 @@ def ToVacuum(l): return l_prev def get_modname(t,g,z): - st = str(int(t)) - if t<10000: - st = '0'+st - sg = '+'+str(np.around(g,1)) - if z < 0: - sz = 'm' - else: - sz = 'p' - z=float(z) - sz = sz + str(np.around(np.absolute(z),1)) - sz = sz.replace('.','') - return 't'+st+'_g'+sg+'_'+sz+'p00_hr' + st = str(int(t)) + if t<10000: + st = '0'+st + sg = '+'+str(np.around(g,1)) + if z < 0: + sz = 'm' + else: + sz = 'p' + z=float(z) + sz = sz + str(np.around(np.absolute(z),1)) + sz = sz.replace('.','') + return 't'+st+'_g'+sg+'_'+sz+'p00_hr' def get_model(t,g,z,model_path='../../data/COELHO2014/'): - modname = model_path + get_modname(t,g,z) - try: - out = pyfits.getdata(modname+'.fits') - except: - out = pyfits.getdata(modname+'plc.fits') - return out + modname = model_path + get_modname(t,g,z) + try: + out = pyfits.getdata(modname+'.fits') + except: + out = pyfits.getdata(modname+'plc.fits') + return out def get_near(x,vec): - if x == vec[0]: - mmin = vec[0] - mmax = vec[1] - elif x == vec[-1]: - mmin = vec[-2] - mmax = vec[-1] - else: - tvec = vec - x - In = np.where(tvec < 0)[0] - mmin = tvec[In].max() + x - Ix = np.where(tvec >= 0)[0] - mmax = tvec[Ix].min() + x - return mmin,mmax + if x == vec[0]: + mmin = vec[0] + mmax = vec[1] + elif x == vec[-1]: + mmin = vec[-2] + mmax = vec[-1] + else: + tvec = vec - x + In = np.where(tvec < 0)[0] + mmin = tvec[In].max() + x + Ix = np.where(tvec >= 0)[0] + mmax = tvec[Ix].min() + x + return mmin,mmax def trilinear_interpolation(t,g,z,model_path='../../data/COELHO2014/'): - teffs = np.arange(6000,10001,250) - loggs = np.arange(2.5,4.6,0.5) - fehs = np.array([-1.,-0.5,0.0,0.2]) - x0,x1 = get_near(t,teffs) - y0,y1 = get_near(g,loggs) - z0,z1 = get_near(z,fehs) - xd = (t-x0)/(x1-x0) - yd = (g-y0)/(y1-y0) - zd = (z-z0)/(z1-z0) - - try: - hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'.fits') - except: - hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'plc.fits') - - c000 = get_model(x0,y0,z0,model_path) - c001 = get_model(x0,y0,z1,model_path) - c010 = get_model(x0,y1,z0,model_path) - c100 = get_model(x1,y0,z0,model_path) - c110 = get_model(x1,y1,z0,model_path) - c101 = get_model(x1,y0,z1,model_path) - c011 = get_model(x0,y1,z1,model_path) - c111 = get_model(x1,y1,z1,model_path) - - wav = np.arange(len(c111))*hd['CDELT1'] + hd['CRVAL1'] - - c00 = c000*(1-xd) + c100*xd - c01 = c001*(1-xd) + c101*xd - c10 = c010*(1-xd) + c110*xd - c11 = c011*(1-xd) + c111*xd - - c0 = c00*(1-yd) + c10*yd - c1 = c01*(1-yd) + c11*yd - - c = c0*(1-zd) + c1*zd - - return wav,c + teffs = np.arange(6000,10001,250) + loggs = np.arange(2.5,4.6,0.5) + fehs = np.array([-1.,-0.5,0.0,0.2]) + x0,x1 = get_near(t,teffs) + y0,y1 = get_near(g,loggs) + z0,z1 = get_near(z,fehs) + xd = (t-x0)/(x1-x0) + yd = (g-y0)/(y1-y0) + zd = (z-z0)/(z1-z0) + + try: + hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'.fits') + except: + hd = pyfits.getheader(model_path+get_modname(x0,y0,z0)+'plc.fits') + + c000 = get_model(x0,y0,z0,model_path) + c001 = get_model(x0,y0,z1,model_path) + c010 = get_model(x0,y1,z0,model_path) + c100 = get_model(x1,y0,z0,model_path) + c110 = get_model(x1,y1,z0,model_path) + c101 = get_model(x1,y0,z1,model_path) + c011 = get_model(x0,y1,z1,model_path) + c111 = get_model(x1,y1,z1,model_path) + + wav = np.arange(len(c111))*hd['CDELT1'] + hd['CRVAL1'] + + c00 = c000*(1-xd) + c100*xd + c01 = c001*(1-xd) + c101*xd + c10 = c010*(1-xd) + c110*xd + c11 = c011*(1-xd) + c111*xd + + c0 = c00*(1-yd) + c10*yd + c1 = c01*(1-yd) + c11*yd + + c = c0*(1-zd) + c1*zd + + return wav,c def normalize_model(w,f): - ow = w.copy() - of = f.copy() - #plot(w,f) - while True: - #medflts = scipy.signal.medfilt(f,1001) - coef = np.polyfit(w,f,6) - fited = np.polyval(coef,w) - res = f - fited - I = np.where(res > -np.sqrt(np.var(res)))[0] - w,f = w[I],f[I] - if len(w) < 0.3* len(ow): - break - #plot(ow,np.polyval(coef,ow)) - #show() - return coef + ow = w.copy() + of = f.copy() + #plot(w,f) + while True: + #medflts = scipy.signal.medfilt(f,1001) + coef = np.polyfit(w,f,6) + fited = np.polyval(coef,w) + res = f - fited + I = np.where(res > -np.sqrt(np.var(res)))[0] + w,f = w[I],f[I] + if len(w) < 0.3* len(ow): + break + #plot(ow,np.polyval(coef,ow)) + #show() + return coef def spec_ccf(sw,sf,mw,mf,vi,vf,dv): - mf = mf -1 - mf = -mf - #plot(mw,mf) - tck = interpolate.splrep(mw,mf,k=1) - v = vi - retccf = [] - vels = [] - while v<=vf: - swt = sw * (1 + v/299792.458) - mft = interpolate.splev(swt,tck) - #if v == 0: - # plot(swt,mft) - # plot(swt,sft) - # show() - mft -= np.mean(mft) - sft = sf - np.mean(sf) - #sft = sf.copy() - #print np.sum(mft**2),np.sum(sft**2) - retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) - vels.append(v) - v+=dv - - return np.array(vels),np.array(retccf) + mf = mf -1 + mf = -mf + #plot(mw,mf) + tck = interpolate.splrep(mw,mf,k=1) + v = vi + retccf = [] + vels = [] + while v<=vf: + swt = sw * (1 + v/299792.458) + mft = interpolate.splev(swt,tck) + #if v == 0: + # plot(swt,mft) + # plot(swt,sft) + # show() + mft -= np.mean(mft) + sft = sf - np.mean(sf) + #sft = sf.copy() + #print np.sum(mft**2),np.sum(sft**2) + retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) + vels.append(v) + v+=dv + + return np.array(vels),np.array(retccf) def ccf_fft(swt,sft,mwt,mft): - mf = mft -1 - mf = -mf + mf = mft -1 + mf = -mf - #plot(mw,mf) - tck = interpolate.splrep(np.log(mwt),mf,k=1) + #plot(mw,mf) + tck = interpolate.splrep(np.log(mwt),mf,k=1) - sw = np.log(swt) - tck2 = interpolate.splrep(sw,sft,k=1) + sw = np.log(swt) + tck2 = interpolate.splrep(sw,sft,k=1) - nsw = np.linspace(sw[0], sw[-1], 5000) - sf = interpolate.splev(nsw,tck2) + nsw = np.linspace(sw[0], sw[-1], 5000) + sf = interpolate.splev(nsw,tck2) - mf = interpolate.splev(nsw,tck) + mf = interpolate.splev(nsw,tck) - sf -= np.mean(sf) - mf -= np.mean(mf) - plot(nsw,sf) - plot(nsw,mf) - show() - retccf = np.fft.ifft(np.conj(np.fft.fft(sf))*np.fft.fft(mf)) - retccf = np.hstack((retccf[2500:],retccf[:2500])) - retvels = np.arange(len(retccf)) - 0.5*len(retccf) - retvels *= (nsw[1]-nsw[0]) - retvels = 299792.458*(np.exp(retvels)-1.) - return retvels, retccf + sf -= np.mean(sf) + mf -= np.mean(mf) + plot(nsw,sf) + plot(nsw,mf) + show() + retccf = np.fft.ifft(np.conj(np.fft.fft(sf))*np.fft.fft(mf)) + retccf = np.hstack((retccf[2500:],retccf[:2500])) + retvels = np.arange(len(retccf)) - 0.5*len(retccf) + retvels *= (nsw[1]-nsw[0]) + retvels = 299792.458*(np.exp(retvels)-1.) + return retvels, retccf def ccf_simple(sw,sf,mw,mf,rv): - mf = mf -1 - mf = -mf - #plot(mw,mf) - tck = interpolate.splrep(mw,mf,k=1) - swt = sw * (1 + rv/299792.458) - mft = interpolate.splev(swt,tck) - mft -= np.mean(mft) - sft = sf - np.mean(sf) - return np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2)) + mf = mf -1 + mf = -mf + #plot(mw,mf) + tck = interpolate.splrep(mw,mf,k=1) + swt = sw * (1 + rv/299792.458) + mft = interpolate.splev(swt,tck) + mft -= np.mean(mft) + sft = sf - np.mean(sf) + return np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2)) def clean_strong_lines(mw,sc,mode=1): - if mode==1: - #"""" - I = np.where((mw>6520)&(mw<6600))[0] - sc[I] = 1. - I = np.where((mw>5888)&(mw<5897))[0] - sc[I] = 1. - I = np.where((mw>4310)&(mw<4360))[0] - sc[I] = 1. - I = np.where((mw>4840)&(mw<4880))[0] - sc[I] = 1. - I = np.where((mw>4070)&(mw<4130))[0] - sc[I] = 1. - I = np.where((mw>3875)&(mw<3900))[0] - sc[I] = 1. - I = np.where((mw>3920)&(mw<3945))[0] - sc[I] = 1. - I = np.where((mw>3955)&(mw<3980))[0] - sc[I] = 1. - I = np.where(mw<3850)[0] - sc[I] = 1. - #""" - if mode==2: - #"""" - I = np.where((mw>6550)&(mw<6570))[0] - sc[I] = 1. - I = np.where((mw>5888)&(mw<5897))[0] - sc[I] = 1. - I = np.where((mw>4320)&(mw<4350))[0] - sc[I] = 1. - I = np.where((mw>4850)&(mw<4870))[0] - sc[I] = 1. - I = np.where((mw>4090)&(mw<4110))[0] - sc[I] = 1. - I = np.where((mw>3875)&(mw<3900))[0] - sc[I] = 1. - I = np.where((mw>3920)&(mw<3945))[0] - sc[I] = 1. - I = np.where((mw>3955)&(mw<3980))[0] - sc[I] = 1. - I = np.where(mw<3850)[0] - sc[I] = 1. - #""" - return sc + if mode==1: + #"""" + I = np.where((mw>6520)&(mw<6600))[0] + sc[I] = 1. + I = np.where((mw>5888)&(mw<5897))[0] + sc[I] = 1. + I = np.where((mw>4310)&(mw<4360))[0] + sc[I] = 1. + I = np.where((mw>4840)&(mw<4880))[0] + sc[I] = 1. + I = np.where((mw>4070)&(mw<4130))[0] + sc[I] = 1. + I = np.where((mw>3875)&(mw<3900))[0] + sc[I] = 1. + I = np.where((mw>3920)&(mw<3945))[0] + sc[I] = 1. + I = np.where((mw>3955)&(mw<3980))[0] + sc[I] = 1. + I = np.where(mw<3850)[0] + sc[I] = 1. + #""" + if mode==2: + #"""" + I = np.where((mw>6550)&(mw<6570))[0] + sc[I] = 1. + I = np.where((mw>5888)&(mw<5897))[0] + sc[I] = 1. + I = np.where((mw>4320)&(mw<4350))[0] + sc[I] = 1. + I = np.where((mw>4850)&(mw<4870))[0] + sc[I] = 1. + I = np.where((mw>4090)&(mw<4110))[0] + sc[I] = 1. + I = np.where((mw>3875)&(mw<3900))[0] + sc[I] = 1. + I = np.where((mw>3920)&(mw<3945))[0] + sc[I] = 1. + I = np.where((mw>3955)&(mw<3980))[0] + sc[I] = 1. + I = np.where(mw<3850)[0] + sc[I] = 1. + #""" + return sc def RVforFR(wavs,flxs,teff=6700,logg=4.0,feh=-1.0,vsini=100.,model_path='../../data/COELHO2014/',vmin=-1000.,vmax=1000.,vstep=10.): - def fitfunc(p,x): - ret = p[3] + p[0] * np.exp(-.5*((x-p[1])/p[2])**2) - return ret - errfunc = lambda p,x,y: np.ravel( (fitfunc(p,x)-y) ) - - #sc = get_model(teff,logg,feh) - #hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') - #wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] - teff = float(teff) - - try: - sc = get_model(teff,logg,feh) - hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') - mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] - except: - mw,sc = trilinear_interpolation(teff,logg,feh,model_path) - - for order in range(len(flxs)): - flxs[order] = clean_strong_lines(wavs[order],flxs[order]) - - sc = clean_strong_lines(mw,sc) - - II = np.where(sc != 1)[0] - JJ = np.where(sc == 1)[0] - - coef = normalize_model(mw[II],sc[II]) - sc /= np.polyval(coef,mw) - sc[JJ] = 1. - mw = ToVacuum(mw) - weis1 = [] - ccftot = [] - for i in range(wavs.shape[0]): - #plot(wavs[i],flxs[i]) - scf = flxs[i] - scw = wavs[i] - - J = np.where(scf!=0)[0] - scw,scf = scw[J],scf[J] - I = np.where((mw>scw[0]-100) & (mw100: - ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) - #plot(ccv,ccf) - #show() - #ccf = np.array(ccf) - wei1 = len(np.where(scf!=1)[0])**2 - weis1.append(wei1) - if len(ccftot)==0: - ccftot = ccf.copy()*wei1 - else: - ccftot = np.vstack((ccftot,ccf.copy()*wei1)) - #show() - weis1 = np.array(weis1) - ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) - - p0 = [ccftot.min(),ccv[np.argmin(ccftot)],vsini,ccftot[0]] - p1, success = scipy.optimize.leastsq(errfunc,p0, args=(ccv,ccftot)) - - return p1,ccv,ccftot,fitfunc(p1,ccv) - + def fitfunc(p,x): + ret = p[3] + p[0] * np.exp(-.5*((x-p[1])/p[2])**2) + return ret + errfunc = lambda p,x,y: np.ravel( (fitfunc(p,x)-y) ) + + #sc = get_model(teff,logg,feh) + #hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') + #wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] + teff = float(teff) + + try: + sc = get_model(teff,logg,feh) + hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') + mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] + except: + mw,sc = trilinear_interpolation(teff,logg,feh,model_path) + + for order in range(len(flxs)): + flxs[order] = clean_strong_lines(wavs[order],flxs[order]) + + sc = clean_strong_lines(mw,sc) + + II = np.where(sc != 1)[0] + JJ = np.where(sc == 1)[0] + + coef = normalize_model(mw[II],sc[II]) + sc /= np.polyval(coef,mw) + sc[JJ] = 1. + mw = ToVacuum(mw) + weis1 = [] + ccftot = [] + for i in range(wavs.shape[0]): + #plot(wavs[i],flxs[i]) + scf = flxs[i] + scw = wavs[i] + + J = np.where(scf!=0)[0] + scw,scf = scw[J],scf[J] + I = np.where((mw>scw[0]-100) & (mw100: + ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) + #plot(ccv,ccf) + #show() + #ccf = np.array(ccf) + wei1 = len(np.where(scf!=1)[0])**2 + weis1.append(wei1) + if len(ccftot)==0: + ccftot = ccf.copy()*wei1 + else: + ccftot = np.vstack((ccftot,ccf.copy()*wei1)) + #show() + weis1 = np.array(weis1) + ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) + + p0 = [ccftot.min(),ccv[np.argmin(ccftot)],vsini,ccftot[0]] + p1, success = scipy.optimize.leastsq(errfunc,p0, args=(ccv,ccftot)) + + return p1,ccv,ccftot,fitfunc(p1,ccv) + def calc_bss2(vels,xc,coef, bot_i=0.15, bot_f=0.4, top_i=0.6, top_f=0.9, dt=0.01): - try: - - I1 = np.where((vels>coef[1]-3*coef[2]) & (velscoef[1]) )[0] - I3 = np.where(velscoef[1]+4*coef[2])[0] - I = np.hstack((I3,I4)) - base = np.median(xc[I]) - - xc = base - xc - xc /= xc.max() - - - v1,x1 = vels[I1],xc[I1] - v2,x2 = vels[I2],xc[I2] - #plot(v1,x1) - #plot(v2,x2) - #show() - dp = top_f - vect = [] - while dp >= top_i: - lb = np.where(x1>dp)[0][0] - m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) - n = v1[lb] - m*x1[lb] - bs1 = m*dp+n - - lb = np.where(x2>dp)[0][-1] - m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) - n = v2[lb] - m*x2[lb] - bs2 = m*dp+n - vect.append(0.5*(bs2+bs1)) - dp-=dt - vect = np.array(vect) - - dp = bot_f - vecb = [] - while dp >= bot_i: - - lb = np.where(x1>dp)[0][0] - m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) - n = v1[lb] - m*x1[lb] - bs1 = m*dp+n - - lb = np.where(x2>dp)[0][-1] - m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) - n = v2[lb] - m*x2[lb] - bs2 = m*dp+n - vecb.append(0.5*(bs2+bs1)) - dp-=dt - vecb = np.array(vecb) - - return np.median(vecb) - np.median(vect) - except: - return -999.0 + try: + + I1 = np.where((vels>coef[1]-3*coef[2]) & (velscoef[1]) )[0] + I3 = np.where(velscoef[1]+4*coef[2])[0] + I = np.hstack((I3,I4)) + base = np.median(xc[I]) + + xc = base - xc + xc /= xc.max() + + + v1,x1 = vels[I1],xc[I1] + v2,x2 = vels[I2],xc[I2] + #plot(v1,x1) + #plot(v2,x2) + #show() + dp = top_f + vect = [] + while dp >= top_i: + lb = np.where(x1>dp)[0][0] + m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) + n = v1[lb] - m*x1[lb] + bs1 = m*dp+n + + lb = np.where(x2>dp)[0][-1] + m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) + n = v2[lb] - m*x2[lb] + bs2 = m*dp+n + vect.append(0.5*(bs2+bs1)) + dp-=dt + vect = np.array(vect) + + dp = bot_f + vecb = [] + while dp >= bot_i: + + lb = np.where(x1>dp)[0][0] + m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) + n = v1[lb] - m*x1[lb] + bs1 = m*dp+n + + lb = np.where(x2>dp)[0][-1] + m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) + n = v2[lb] - m*x2[lb] + bs2 = m*dp+n + vecb.append(0.5*(bs2+bs1)) + dp-=dt + vecb = np.array(vecb) + + return np.median(vecb) - np.median(vect) + except: + return -999.0 """ def lnlike(theta, W, F, Ferr): - mw,sc = trilinear_interpolation(int(theta[0]),theta[1],theta[2]) - sct = clean_strong_lines(mw,sc.copy()) - #plot(mw,sc) - #show() - coef = normalize_model(mw,sct) - sc /= np.polyval(coef,mw) - - #print gfd - mw = ToVacuum(mw) - mw *= 1 + theta[3]/299792.458 - - totD,totM,totE = np.array([]),np.array([]),np.array([]) - for i in range(W.shape[0]): - scf = F[i] - scw = W[i] - scfe = Ferr[i] - - J = np.where(scf!=0)[0] - scw,scf,scfe = scw[J],scf[J],scfe[J] - - I = np.where((mw>scw[0]-10) & (mwscw[0]-10) & (mwscw[0]-100) & (mw100: - ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) - #ccv,ccf = ccf_fft(scw,scf,mw[I],tmf) - #plot(ccv,ccf) - #show() - - wei1 = len(np.where(scf!=1)[0])**2 - weis1.append(wei1) - if len(ccftot)==0: - ccftot = ccf.copy()*wei1 - else: - ccftot = np.vstack((ccftot,ccf.copy()*wei1)) - weis1 = np.array(weis1) - ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) - - - #print gfds - #ccftot = np.mean(ccftot,axis=0) - #print pars, ccftot.min() - return ccftot.min() + teff,logg,feh,vsini=pars[0],pars[1],pars[2],pars[3] + vmin=-500 + vmax=500. + vstep=20. + sc = get_model(teff,logg,feh) + hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') + wav = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] + try: + sc = get_model(teff,logg,feh) + hd = pyfits.getheader(model_path+get_modname(7000,4.5,0.0)+'.fits') + mw = np.arange(len(sc))*hd['CDELT1'] + hd['CRVAL1'] + except: + mw,sc = trilinear_interpolation(teff,logg,feh,model_path) + + sc = clean_strong_lines(mw,sc) + + II = np.where(sc != 1)[0] + JJ = np.where(sc == 1)[0] + + coef = normalize_model(mw[II],sc[II]) + sc /= np.polyval(coef,mw) + sc[JJ] = 1. + mw = ToVacuum(mw) + weis1 = [] + ccftot = [] + for i in range(wavs.shape[0]): + scf = flxs[i].copy() + scw = wavs[i].copy() + + J = np.where(scf!=0)[0] + scw,scf = scw[J],scf[J] + I = np.where((mw>scw[0]-100) & (mw100: + ccv,ccf = spec_ccf(scw,scf,mw[I],tmf,vmin,vmax,vstep) + #ccv,ccf = ccf_fft(scw,scf,mw[I],tmf) + #plot(ccv,ccf) + #show() + + wei1 = len(np.where(scf!=1)[0])**2 + weis1.append(wei1) + if len(ccftot)==0: + ccftot = ccf.copy()*wei1 + else: + ccftot = np.vstack((ccftot,ccf.copy()*wei1)) + weis1 = np.array(weis1) + ccftot = np.sum(ccftot,axis=0)/ np.sum(weis1) + + + #print gfds + #ccftot = np.mean(ccftot,axis=0) + #print pars, ccftot.min() + return ccftot.min() def get_pars_fr(wavst,flxst,model_patht='../../data/COELHO2014/',npools=4,fixG=1.0): - for order in range(len(flxst)): - flxst[order] = clean_strong_lines(wavst[order],flxst[order],mode=1) - - t0 = time.time() - - global wavs,flxs - global model_path - - wavs,flxs=wavst.copy(),flxst.copy() - model_path=model_patht - - - gt = np.array([6000,7000,8000,9000,10000]) - gg = np.array([2.5,3.0,3.5,4.0,4.5]) - if fixG != -1: - gg = np.array([fixG]) - gz = np.array([-1,-0.5,0.0,0.2]) - gr = np.array([10.,50.,100.,150.,200.,250.,300.]) - - #""" - tr = np.tile(gr,len(gt)*len(gg)*len(gz)) - tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) - tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) - tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) - tot = np.vstack((tt,tg,tz,tr)).T - - #for pars in tot: - # pars = [8000,4.0,-0.5,40.0] - # print pars, multiccf(pars) - - p = Pool(npools) - vals = np.array((p.map(multiccf, list(tot)))) - p.terminate() - I = np.argmin(vals) - best_vals = tot[I] - bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] - #""" - t1 = time.time() - print bt,bg,bz,br, (t1-t0)/60.,'mins' - - - #bt,bg,bz,br = 7000.,4.5, 0.2, 100.0 - gt = np.arange(bt-1000,bt+1001,250) - I = np.where((gt>=6000) & (gt<=10000))[0] - gt = gt[I] - gr = np.arange(br-60.,br+61.,20.) - I = np.where(gr>=10)[0] - gr = gr[I] - - tr = np.tile(gr,len(gt)*len(gg)*len(gz)) - tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) - tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) - tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) - tot = np.vstack((tt,tg,tz,tr)).T - - p = Pool(npools) - vals = np.array((p.map(multiccf, list(tot)))) - p.terminate() - I = np.argmin(vals) - best_vals = tot[I] - bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] - t2 = time.time() - print bt,bg,bz,br, (t2-t1)/60.,'mins' - #np.savetxt('temp_grid.txt',vals) - - - if fixG==-1: - grid = np.reshape(vals,(len(gt),len(gg),len(gz),len(gr))) - tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) - tckg = interpolate.splrep(gg,np.arange(len(gg)),k=1) - tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) - tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) - - itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) - itckg = interpolate.splrep(np.arange(len(gg)),gg,k=1) - itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) - itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) - - st = np.arange(gt[0],gt[-1]+1,10.) - sg = np.arange(gg[0],gg[-1]+0.01,0.1) - sz = np.arange(gz[0],gz[-1]+0.01,0.1) - sr = np.arange(gr[0],gr[-1]+1.,5.) - - st = interpolate.splev(st,tckt) - sg = interpolate.splev(sg,tckg) - sz = interpolate.splev(sz,tckz) - sr = interpolate.splev(sr,tckr) - - tr2 = np.tile(sr,len(st)*len(sg)*len(sz)) - tg2 = np.repeat(np.tile(sg,len(st)),len(sr)*len(sz)) - tz2 = np.repeat(np.tile(sz,len(st)*len(sg)),len(sr)) - tt2 = np.repeat(st,len(sg)*len(sr)*len(sz)) - tot2 = np.vstack((tt2,tg2,tz2,tr2)) - - zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') - I = np.argmin(zi) - minval = tot2[:,I] - - mint = interpolate.splev(minval[0],itckt) - ming = interpolate.splev(minval[1],itckg) - minz = interpolate.splev(minval[2],itckz) - minr = interpolate.splev(minval[3],itckr) - - else: - grid = np.reshape(vals,(len(gt),len(gz),len(gr))) - tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) - tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) - tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) - - itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) - itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) - itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) - - st = np.arange(gt[0],gt[-1]+1,10.) - sz = np.arange(gz[0],gz[-1]+0.01,0.1) - sr = np.arange(gr[0],gr[-1]+1.,5.) - - st = interpolate.splev(st,tckt) - sz = interpolate.splev(sz,tckz) - sr = interpolate.splev(sr,tckr) - - tr2 = np.tile(sr,len(st)*len(sz)) - tz2 = np.repeat(np.tile(sz,len(st)),len(sr)) - tt2 = np.repeat(st,len(sr)*len(sz)) - tot2 = np.vstack((tt2,tz2,tr2)) - - zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') - I = np.argmin(zi) - minval = tot2[:,I] - - mint = interpolate.splev(minval[0],itckt) - ming = fixG - minz = interpolate.splev(minval[1],itckz) - minr = interpolate.splev(minval[2],itckr) - - #d = {'grid':grid, 'zi':zi, 'tot2':tot2, 'gt':gt, 'gg':gg, 'gz':gz, 'gr':gr} - #pickle.dump(d,open('temp_dict.pkl')) - - return float(mint),float(ming),float(minz),float(minr) + for order in range(len(flxst)): + flxst[order] = clean_strong_lines(wavst[order],flxst[order],mode=1) + + t0 = time.time() + + global wavs,flxs + global model_path + + wavs,flxs=wavst.copy(),flxst.copy() + model_path=model_patht + + + gt = np.array([6000,7000,8000,9000,10000]) + gg = np.array([2.5,3.0,3.5,4.0,4.5]) + if fixG != -1: + gg = np.array([fixG]) + gz = np.array([-1,-0.5,0.0,0.2]) + gr = np.array([10.,50.,100.,150.,200.,250.,300.]) + + #""" + tr = np.tile(gr,len(gt)*len(gg)*len(gz)) + tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) + tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) + tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) + tot = np.vstack((tt,tg,tz,tr)).T + + #for pars in tot: + # pars = [8000,4.0,-0.5,40.0] + # print pars, multiccf(pars) + + p = Pool(npools) + vals = np.array((p.map(multiccf, list(tot)))) + p.terminate() + I = np.argmin(vals) + best_vals = tot[I] + bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] + #""" + t1 = time.time() + print(bt,bg,bz,br, (t1-t0)/60.,'mins') + + + #bt,bg,bz,br = 7000.,4.5, 0.2, 100.0 + gt = np.arange(bt-1000,bt+1001,250) + I = np.where((gt>=6000) & (gt<=10000))[0] + gt = gt[I] + gr = np.arange(br-60.,br+61.,20.) + I = np.where(gr>=10)[0] + gr = gr[I] + + tr = np.tile(gr,len(gt)*len(gg)*len(gz)) + tg = np.repeat(np.tile(gg,len(gt)),len(gr)*len(gz)) + tz = np.repeat(np.tile(gz,len(gt)*len(gg)),len(gr)) + tt = np.repeat(gt,len(gg)*len(gr)*len(gz)) + tot = np.vstack((tt,tg,tz,tr)).T + + p = Pool(npools) + vals = np.array((p.map(multiccf, list(tot)))) + p.terminate() + I = np.argmin(vals) + best_vals = tot[I] + bt,bg,bz,br = best_vals[0],best_vals[1],best_vals[2],best_vals[3] + t2 = time.time() + print(bt,bg,bz,br, (t2-t1)/60.,'mins') + #np.savetxt('temp_grid.txt',vals) + + + if fixG==-1: + grid = np.reshape(vals,(len(gt),len(gg),len(gz),len(gr))) + tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) + tckg = interpolate.splrep(gg,np.arange(len(gg)),k=1) + tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) + tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) + + itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) + itckg = interpolate.splrep(np.arange(len(gg)),gg,k=1) + itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) + itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) + + st = np.arange(gt[0],gt[-1]+1,10.) + sg = np.arange(gg[0],gg[-1]+0.01,0.1) + sz = np.arange(gz[0],gz[-1]+0.01,0.1) + sr = np.arange(gr[0],gr[-1]+1.,5.) + + st = interpolate.splev(st,tckt) + sg = interpolate.splev(sg,tckg) + sz = interpolate.splev(sz,tckz) + sr = interpolate.splev(sr,tckr) + + tr2 = np.tile(sr,len(st)*len(sg)*len(sz)) + tg2 = np.repeat(np.tile(sg,len(st)),len(sr)*len(sz)) + tz2 = np.repeat(np.tile(sz,len(st)*len(sg)),len(sr)) + tt2 = np.repeat(st,len(sg)*len(sr)*len(sz)) + tot2 = np.vstack((tt2,tg2,tz2,tr2)) + + zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') + I = np.argmin(zi) + minval = tot2[:,I] + + mint = interpolate.splev(minval[0],itckt) + ming = interpolate.splev(minval[1],itckg) + minz = interpolate.splev(minval[2],itckz) + minr = interpolate.splev(minval[3],itckr) + + else: + grid = np.reshape(vals,(len(gt),len(gz),len(gr))) + tckt = interpolate.splrep(gt,np.arange(len(gt)),k=1) + tckz = interpolate.splrep(gz,np.arange(len(gz)),k=1) + tckr = interpolate.splrep(gr,np.arange(len(gr)),k=1) + + itckt = interpolate.splrep(np.arange(len(gt)),gt,k=1) + itckz = interpolate.splrep(np.arange(len(gz)),gz,k=1) + itckr = interpolate.splrep(np.arange(len(gr)),gr,k=1) + + st = np.arange(gt[0],gt[-1]+1,10.) + sz = np.arange(gz[0],gz[-1]+0.01,0.1) + sr = np.arange(gr[0],gr[-1]+1.,5.) + + st = interpolate.splev(st,tckt) + sz = interpolate.splev(sz,tckz) + sr = interpolate.splev(sr,tckr) + + tr2 = np.tile(sr,len(st)*len(sz)) + tz2 = np.repeat(np.tile(sz,len(st)),len(sr)) + tt2 = np.repeat(st,len(sr)*len(sz)) + tot2 = np.vstack((tt2,tz2,tr2)) + + zi = ndimage.map_coordinates(grid, tot2, order=3, mode='nearest') + I = np.argmin(zi) + minval = tot2[:,I] + + mint = interpolate.splev(minval[0],itckt) + ming = fixG + minz = interpolate.splev(minval[1],itckz) + minr = interpolate.splev(minval[2],itckr) + + #d = {'grid':grid, 'zi':zi, 'tot2':tot2, 'gt':gt, 'gg':gg, 'gz':gz, 'gr':gr} + #pickle.dump(d,open('temp_dict.pkl')) + + return float(mint),float(ming),float(minz),float(minr) def plot_CCF_FR(xc_dict,path='XC.pdf'): - vels = xc_dict['vels'] - xc_av = xc_dict['xc_av'] - XCmodelgau = xc_dict['XCmodelgau'] - #refvel = xc_dict['refvel'] - p1gau = xc_dict['p1gau'] - - f1 = figure() - - pp = PdfPages(path) - ax1 = f1.add_subplot(111) - ax1.plot(vels, xc_av,'b.', label='CCF') - ax1.plot(vels, XCmodelgau,'r-',label='Gaussian fit') - - xlabel('Velocity (km/s)') - ylabel('XC') - ax1.axvline(p1gau[1],linestyle=':',color='r') - ax1.axhline(0.0,linestyle='-') - title('Average Cross-Correlation Function + Fit') - handles, labels = ax1.get_legend_handles_labels() - ax1.legend(handles[::-1], labels[::-1],prop={'size':6}) - pp.savefig() - pp.close() - clf() - pass + vels = xc_dict['vels'] + xc_av = xc_dict['xc_av'] + XCmodelgau = xc_dict['XCmodelgau'] + #refvel = xc_dict['refvel'] + p1gau = xc_dict['p1gau'] + + f1 = figure() + + pp = PdfPages(path) + ax1 = f1.add_subplot(111) + ax1.plot(vels, xc_av,'b.', label='CCF') + ax1.plot(vels, XCmodelgau,'r-',label='Gaussian fit') + + xlabel('Velocity (km/s)') + ylabel('XC') + ax1.axvline(p1gau[1],linestyle=':',color='r') + ax1.axhline(0.0,linestyle='-') + title('Average Cross-Correlation Function + Fit') + handles, labels = ax1.get_legend_handles_labels() + ax1.legend(handles[::-1], labels[::-1],prop={'size':6}) + pp.savefig() + pp.close() + clf() + pass """ def trans_chromosome(chromosome): - teff = chromosome[0]*100.+chromosome[1]*10.+chromosome[2] - m = (10000.- 6000.)/999. - n = 6000. - teff = teff*m + n - logg = chromosome[3] + chromosome[4]*0.1 - m = (4.5 - 3.0)/9.9 - n = 3. - logg = logg*m + n - - feh = chromosome[5] + chromosome[6]*0.1 - m = (0.2 - -1.)/9.9 - n = -1. - feh = feh*m + n - - vsini = chromosome[7]*10. + chromosome[8] - m = (300. - 10.)/99. - n = 10. - vsini = vsini*m + n - - return teff, logg, feh, vsini + teff = chromosome[0]*100.+chromosome[1]*10.+chromosome[2] + m = (10000.- 6000.)/999. + n = 6000. + teff = teff*m + n + logg = chromosome[3] + chromosome[4]*0.1 + m = (4.5 - 3.0)/9.9 + n = 3. + logg = logg*m + n + + feh = chromosome[5] + chromosome[6]*0.1 + m = (0.2 - -1.)/9.9 + n = -1. + feh = feh*m + n + + vsini = chromosome[7]*10. + chromosome[8] + m = (300. - 10.)/99. + n = 10. + vsini = vsini*m + n + + return teff, logg, feh, vsini global wavs, flxs def find_pars_GA(wavs,flxs,model_path='../../data/COELHO2014/'): - def eval_func(chromosome): - print list(chromosome) - teff, logg, feh, vsini = trans_chromosome(chromosome) - print teff, logg, feh, vsini - pt,vels,ccf,mod = RVforFR(wavs,flxs,teff=teff,logg=logg,feh=feh,vsini=vsini,model_path=model_path) - score = -ccf.min() - return score - - genome = G1DList.G1DList(9) - genome.evaluator.set(eval_func) - - ga = GSimpleGA.GSimpleGA(genome, interactiveMode=True) - ga.setGenerations(40) - ga.setMutationRate(0.2) - ga.setPopulationSize(20) - #ga.setCrossoverRate(1.0) - genome.setParams(rangemin=0, rangemax=9) - #ga.setMultiProcessing(True) - ga.evolve(freq_stats=10) - print ga.bestIndividual() - print trans_chromosome(ga.bestIndividual()) + def eval_func(chromosome): + print list(chromosome) + teff, logg, feh, vsini = trans_chromosome(chromosome) + print teff, logg, feh, vsini + pt,vels,ccf,mod = RVforFR(wavs,flxs,teff=teff,logg=logg,feh=feh,vsini=vsini,model_path=model_path) + score = -ccf.min() + return score + + genome = G1DList.G1DList(9) + genome.evaluator.set(eval_func) + + ga = GSimpleGA.GSimpleGA(genome, interactiveMode=True) + ga.setGenerations(40) + ga.setMutationRate(0.2) + ga.setPopulationSize(20) + #ga.setCrossoverRate(1.0) + genome.setParams(rangemin=0, rangemax=9) + #ga.setMultiProcessing(True) + ga.evolve(freq_stats=10) + print ga.bestIndividual() + print trans_chromosome(ga.bestIndividual()) """ - - - diff --git a/utils/GLOBALutils/GLOBALutils.py b/utils/GLOBALutils/GLOBALutils.py index 095a1e9..8f173a0 100644 --- a/utils/GLOBALutils/GLOBALutils.py +++ b/utils/GLOBALutils/GLOBALutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use('Agg') @@ -13,7 +14,10 @@ import matplotlib.pyplot as plt import sys import tempfile -import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO import string import pycurl from multiprocessing import Pool @@ -34,29 +38,29 @@ # Some functions to be used by the pipeline class Constants: - "Here I declare the constants I will use in the different functions" - "G,c: the gravity and speed of light constants in SI units; mearth and mmoon: the earth and moon masses in kg" - G,c = 6.673E-11,2.99792458E8 - mearth, mmoon = 5.9736E24,7.349E22 - "the mass of the planets,moon and sun in earth masses, the ennumeration is sun, moon, mercury,venus,mars,jupiter,saturn, uranus,neptune,pluto" - mplanets = np.array([332981.787,0.0123000371,0.05528,0.815,0.10745,317.83,95.19,14.536,17.147,0.0021]) - "conversion from degrees to radians, and from hours to degrees, and from degrees to hours" - degtorad = np.pi/180.0 - radtodeg = 180.0/np.pi - HtoDeg = 360.0/24.0 - DegtoH = 1.0/HtoDeg - "Req: equatorial radius f: polar flatenning , w angular speedof earth in rad/s" - Req = 6378136.6 #in m - f = 1.0/298.256420 - w = 7.2921158554E-5 - DaystoYear = 1.0/365.256363004 + "Here I declare the constants I will use in the different functions" + "G,c: the gravity and speed of light constants in SI units; mearth and mmoon: the earth and moon masses in kg" + G,c = 6.673E-11,2.99792458E8 + mearth, mmoon = 5.9736E24,7.349E22 + "the mass of the planets,moon and sun in earth masses, the ennumeration is sun, moon, mercury,venus,mars,jupiter,saturn, uranus,neptune,pluto" + mplanets = np.array([332981.787,0.0123000371,0.05528,0.815,0.10745,317.83,95.19,14.536,17.147,0.0021]) + "conversion from degrees to radians, and from hours to degrees, and from degrees to hours" + degtorad = np.pi/180.0 + radtodeg = 180.0/np.pi + HtoDeg = 360.0/24.0 + DegtoH = 1.0/HtoDeg + "Req: equatorial radius f: polar flatenning , w angular speedof earth in rad/s" + Req = 6378136.6 #in m + f = 1.0/298.256420 + w = 7.2921158554E-5 + DaystoYear = 1.0/365.256363004 def update_header(hdu,k,v,c=''): - try: - hdu.header.update(k,v) - except: - hdu.header[k] = v - return hdu + try: + hdu.header.update(k,v) + except: + hdu.header[k] = v + return hdu def get_dark_times(darks,key='EXPTIME'): @@ -67,14 +71,14 @@ def get_dark_times(darks,key='EXPTIME'): times = [] for dark in darks: hd = pyfits.getheader(dark) - times.append(hd[key]) + times.append(hd[key]) times = np.sort(np.unique(np.array(times))) return times def get_tdarks(darks,dtime,key='EXPTIME'): tdarks = [] for dark in darks: - hd = pyfits.getheader(dark) + hd = pyfits.getheader(dark) if hd[key] == dtime: tdarks.append(dark) return tdarks @@ -105,60 +109,60 @@ def ToVacuum(l): return l_prev def getcoords(obname,mjd,filen='/data/echelle/feros/coords.txt'): - RA,DEC = 0.,0. - mjdJ2000 = 2451545.0 - 2400000.5 - - try: - f = open(filen,'r') - lines = f.readlines() - f.close() - for line in lines: - cos = line.split(',') - if cos[0]==obname and int(cos[5])==1: - ra,dec = cos[1],cos[2] - cos1 = ra.split(':') - cos2 = dec.split(':') - RA0 = (float(cos1[0]) + float(cos1[1])/60. + float(cos1[2])/3600.) * 360. / 24. - if float(cos2[0]) > 0: - DEC0 = np.absolute(float(cos2[0])) + float(cos2[1])/60. + float(cos2[2])/3600. - else: - DEC0 = -(np.absolute(float(cos2[0])) + float(cos2[1])/60. + float(cos2[2])/3600.) - PMRA = float(cos[3])/1000. - PMDEC = float(cos[4])/1000. - - RA = RA0 + (PMRA/3600.)*(mjd-mjdJ2000)/365. - DEC = DEC0 + (PMDEC/3600.)*(mjd-mjdJ2000)/365. - break - except: - print '\t\tWarning! Problem with reference coordinates files.' - return RA,DEC + RA,DEC = 0.,0. + mjdJ2000 = 2451545.0 - 2400000.5 + + try: + f = open(filen,'r') + lines = f.readlines() + f.close() + for line in lines: + cos = line.split(',') + if cos[0]==obname and int(cos[5])==1: + ra,dec = cos[1],cos[2] + cos1 = ra.split(':') + cos2 = dec.split(':') + RA0 = (float(cos1[0]) + float(cos1[1])/60. + float(cos1[2])/3600.) * 360. / 24. + if float(cos2[0]) > 0: + DEC0 = np.absolute(float(cos2[0])) + float(cos2[1])/60. + float(cos2[2])/3600. + else: + DEC0 = -(np.absolute(float(cos2[0])) + float(cos2[1])/60. + float(cos2[2])/3600.) + PMRA = float(cos[3])/1000. + PMDEC = float(cos[4])/1000. + + RA = RA0 + (PMRA/3600.)*(mjd-mjdJ2000)/365. + DEC = DEC0 + (PMDEC/3600.)*(mjd-mjdJ2000)/365. + break + except: + print('\t\tWarning! Problem with reference coordinates files.') + return RA,DEC def get_them(sc,exap,ncoef,maxords=-1,startfrom=0,nsigmas=10.,mode=1,endat=-1,nc2=2): exap = int(exap) def fitfunc(p,x): - ret = p[0] + p[1] * np.exp(-.5*((x-p[2])/p[3])**2) - return ret + ret = p[0] + p[1] * np.exp(-.5*((x-p[2])/p[3])**2) + return ret errfunc = lambda p,y,x: np.ravel( (fitfunc(p,x)-y) ) def gauss2(params,x): - amp1 = params[0] - amp2 = params[1] - med1 = params[2] - med2 = params[3] - sig1 = params[4] - sig2 = params[5] - g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) + amp1 = params[0] + amp2 = params[1] + med1 = params[2] + med2 = params[3] + sig1 = params[4] + sig2 = params[5] + g1 = amp1 * np.exp(-0.5*((x-med1)/sig1)**2) g2 = amp2 * np.exp(-0.5*((x-med2)/sig2)**2) - return g1 + g2 + return g1 + g2 def res_gauss2(params,g,x): - return g-gauss2(params,x) + return g-gauss2(params,x) sc_or = sc.copy() if endat == -1: - sc = sc[startfrom:,:] + sc = sc[startfrom:,:] else: - sc = sc[startfrom:endat,:] - + sc = sc[startfrom:endat,:] + medc = int(.5*sc.shape[1]) d = np.median(sc[:,medc-exap:medc+exap+1],axis=1) #print sc[:,2000] @@ -171,27 +175,27 @@ def res_gauss2(params,g,x): sigc = .5*exap i = 0 while i < sc.shape[0]: - if i-refw < 0: - x = ejx[:i+refw+1] - y = d[:i+refw+1] - elif i + refw +1 > sc.shape[0]: - x = ejx[i-refw:] - y = d[i-refw:] - else: - x = ejx[i-refw:i+refw+1] - y = d[i-refw:i+refw+1] - - g = np.exp(-0.5*((x-i)/sigc)**2) - ccf.append(np.add.reduce(y*g)) - i+=1 + if i-refw < 0: + x = ejx[:i+refw+1] + y = d[:i+refw+1] + elif i + refw +1 > sc.shape[0]: + x = ejx[i-refw:] + y = d[i-refw:] + else: + x = ejx[i-refw:i+refw+1] + y = d[i-refw:i+refw+1] + + g = np.exp(-0.5*((x-i)/sigc)**2) + ccf.append(np.add.reduce(y*g)) + i+=1 i = 1 maxs = [] while i < len(ccf)-2: - if ccf[i]>ccf[i-1] and ccf[i]>ccf[i+1]: - maxs.append(i) - i+=1 - + if ccf[i]>ccf[i-1] and ccf[i]>ccf[i+1]: + maxs.append(i) + i+=1 + maxs = np.array(maxs) ccf = np.array(ccf) @@ -207,24 +211,24 @@ def res_gauss2(params,g,x): exap2 = exap + int(exap*2./3.) tbase = np.array([]) for i in range(len(pos)): - exs,vec = [],[] - if i == 0: - if pos[i] - exap2 - sp < 0: - exs = np.arange(0, pos[i] - exap2+1,1) - vec = d[: pos[i] - exap2+1] - else: - exs = np.arange(pos[i] - exap2 - sp, pos[i] - exap2+1,1) - vec = d[pos[i] - exap2 - sp: pos[i] - exap2+1] - else: - if pos[i-1] + exap2 < pos[i] - exap2+1: - #print pos[i-1] + exap2 , pos[i] - exap2+1 - exs = np.arange(pos[i-1] + exap2 , pos[i] - exap2+1,1) - vec = d[pos[i-1] + exap2 : pos[i] - exap2 + 1] - - if len(exs)>0: - tbase = np.hstack((tbase,exs)) - vx.append(np.median(exs)) - vy.append(np.median(vec)) + exs,vec = [],[] + if i == 0: + if pos[i] - exap2 - sp < 0: + exs = np.arange(0, pos[i] - exap2+1,1) + vec = d[: pos[i] - exap2+1] + else: + exs = np.arange(pos[i] - exap2 - sp, pos[i] - exap2+1,1) + vec = d[pos[i] - exap2 - sp: pos[i] - exap2+1] + else: + if pos[i-1] + exap2 < pos[i] - exap2+1: + #print pos[i-1] + exap2 , pos[i] - exap2+1 + exs = np.arange(pos[i-1] + exap2 , pos[i] - exap2+1,1) + vec = d[pos[i-1] + exap2 : pos[i] - exap2 + 1] + + if len(exs)>0: + tbase = np.hstack((tbase,exs)) + vx.append(np.median(exs)) + vy.append(np.median(vec)) tbase = tbase.astype('int') vx,vy = np.array(vx),np.array(vy) @@ -265,9 +269,9 @@ def res_gauss2(params,g,x): #print gfd #print len(pos) if maxords >0: - pos = pos[::-1] - pos = pos[:maxords] - pos = pos[::-1] + pos = pos[::-1] + pos = pos[:maxords] + pos = pos[::-1] I = np.where(pos < exap)[0] pos = np.delete(pos,I) @@ -280,52 +284,52 @@ def res_gauss2(params,g,x): #plot(d) ref = [] if mode == 1 or mode == 2: - if mode == 1: - exap2 = exap + .5*exap - dev = exap2/3. - else: - exap2 = exap + .2*exap - dev = exap2/4. - exap2 = int(exap2) - for i in range(len(pos)): - if pos[i]-exap2 < 0: - x = ejx[:int(pos[i]+exap2+1)] - y = d[:int(pos[i]+exap2+1)] - elif pos[i]+exap2+1 > len(d): - x = ejx[int(pos[i]-exap2):] - y = d[int(pos[i]-exap2):] - else: - x = ejx[int(pos[i]-exap2):int(pos[i]+exap2+1)] - y = d[int(pos[i]-exap2):int(pos[i]+exap2+1)] - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) - ty2 = np.zeros(len(tx2)) - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - y -= y.min() - - if mode == 1: - if len(x) < 4: - tref.append(ref[j]) - else: - p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) - ref.append(p[2]) - else: - midi = int(0.5*len(x)) - if len(x) < 7: - tref.append(ref[j]) - else: - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - #print guess - #print p - #plot(x,y) - #axvline(p[2]) - #axvline(p[3]) - #axvline(0.5*(p[2]+p[3])) - #show() - ref.append(0.5*(p[2]+p[3])) + if mode == 1: + exap2 = exap + .5*exap + dev = exap2/3. + else: + exap2 = exap + .2*exap + dev = exap2/4. + exap2 = int(exap2) + for i in range(len(pos)): + if pos[i]-exap2 < 0: + x = ejx[:int(pos[i]+exap2+1)] + y = d[:int(pos[i]+exap2+1)] + elif pos[i]+exap2+1 > len(d): + x = ejx[int(pos[i]-exap2):] + y = d[int(pos[i]-exap2):] + else: + x = ejx[int(pos[i]-exap2):int(pos[i]+exap2+1)] + y = d[int(pos[i]-exap2):int(pos[i]+exap2+1)] + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + ty2 = np.zeros(len(tx2)) + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + y -= y.min() + + if mode == 1: + if len(x) < 4: + tref.append(ref[j]) + else: + p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) + ref.append(p[2]) + else: + midi = int(0.5*len(x)) + if len(x) < 7: + tref.append(ref[j]) + else: + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + #print guess + #print p + #plot(x,y) + #axvline(p[2]) + #axvline(p[3]) + #axvline(0.5*(p[2]+p[3])) + #show() + ref.append(0.5*(p[2]+p[3])) ref = np.array(ref) #plot(d) @@ -334,218 +338,218 @@ def res_gauss2(params,g,x): mat = np.zeros((len(ref),sc.shape[1])) mat[:,medc] = ref i = medc -1 - + while i >=0: - #print i - d = sc[:,i] - j = 0 - pos = np.around(ref).astype('int') - tref = [] - if mode == 1 or mode == 2: - if mode == 1: - exap2 = exap + .5*exap - dev = exap2/3. - else: - exap2 = exap + .2*exap - dev = exap2/4. - exap2 = int(exap2) - while j < len(pos): - if pos[j]-exap2 < 0: - x = ejx[:int(pos[j]+exap2+1)] - y = d[:int(pos[j]+exap2+1)] - elif pos[j]+exap2+1 > len(d): - x = ejx[int(pos[j]-exap2):] - y = d[int(pos[j]-exap2):] - else: - x = ejx[int(pos[j]-exap2):int(pos[j]+exap2+1)] - y = d[int(pos[j]-exap2):int(pos[j]+exap2+1)] - - if mode==1: - if len(x) < 4: - tref.append(ref[j]) - else: - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) + y.min() - ty2 = np.zeros(len(tx2)) + y.min() - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) - #plot(x,y) - #plot(x,fitfunc(p,x)) - tref.append(p[2]) - else: - if len(x) < 7: - tref.append(ref[j]) - else: - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) + y.min() - ty2 = np.zeros(len(tx2)) + y.min() - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - y -= y.min() - midi = int(0.5*len(x)) - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - tref.append(0.5*(p[2]+p[3])) - j+=1 - #show() - oref = ref.copy() - tref = np.array(tref) - dif = tref-ref - coef = np.polyfit(ref,dif,nc2) - #plot(ref,dif,'ro') - #plot(oref,np.polyval(coef,oref)) - coef2 = np.polyfit(np.arange(len(dif)),dif,1) - #if i < 500: - # plot(np.arange(len(dif)),dif,'.') - # plot(np.arange(len(dif)),np.polyval(coef2,np.arange(len(dif)))) - # xlabel('echelle order number',fontsize=18) - # ylabel('shift [px]',fontsize=18) - # show() - # print gfds - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - dif = np.delete(dif,im) - ref = np.delete(ref,im) - coef = np.polyfit(ref,dif,nc2) - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - #plot(oref,np.polyval(coef,oref)) - cdif = np.polyval(coef,oref) - ref = oref + cdif - #plot(ref,np.polyval(coef,ref)) - #show() - - mat[:,i] = ref - i-=4 + #print i + d = sc[:,i] + j = 0 + pos = np.around(ref).astype('int') + tref = [] + if mode == 1 or mode == 2: + if mode == 1: + exap2 = exap + .5*exap + dev = exap2/3. + else: + exap2 = exap + .2*exap + dev = exap2/4. + exap2 = int(exap2) + while j < len(pos): + if pos[j]-exap2 < 0: + x = ejx[:int(pos[j]+exap2+1)] + y = d[:int(pos[j]+exap2+1)] + elif pos[j]+exap2+1 > len(d): + x = ejx[int(pos[j]-exap2):] + y = d[int(pos[j]-exap2):] + else: + x = ejx[int(pos[j]-exap2):int(pos[j]+exap2+1)] + y = d[int(pos[j]-exap2):int(pos[j]+exap2+1)] + + if mode==1: + if len(x) < 4: + tref.append(ref[j]) + else: + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + y.min() + ty2 = np.zeros(len(tx2)) + y.min() + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) + #plot(x,y) + #plot(x,fitfunc(p,x)) + tref.append(p[2]) + else: + if len(x) < 7: + tref.append(ref[j]) + else: + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + y.min() + ty2 = np.zeros(len(tx2)) + y.min() + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + y -= y.min() + midi = int(0.5*len(x)) + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + tref.append(0.5*(p[2]+p[3])) + j+=1 + #show() + oref = ref.copy() + tref = np.array(tref) + dif = tref-ref + coef = np.polyfit(ref,dif,nc2) + #plot(ref,dif,'ro') + #plot(oref,np.polyval(coef,oref)) + coef2 = np.polyfit(np.arange(len(dif)),dif,1) + #if i < 500: + # plot(np.arange(len(dif)),dif,'.') + # plot(np.arange(len(dif)),np.polyval(coef2,np.arange(len(dif)))) + # xlabel('echelle order number',fontsize=18) + # ylabel('shift [px]',fontsize=18) + # show() + # print gfds + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + dif = np.delete(dif,im) + ref = np.delete(ref,im) + coef = np.polyfit(ref,dif,nc2) + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + #plot(oref,np.polyval(coef,oref)) + cdif = np.polyval(coef,oref) + ref = oref + cdif + #plot(ref,np.polyval(coef,ref)) + #show() + + mat[:,i] = ref + i-=4 i = medc+1 ref = mat[:,medc] while i < sc.shape[1]: - #print i - d = sc[:,i] - j = 0 - pos = np.around(ref).astype('int') - tref = [] - if mode == 1 or mode == 2: - if mode == 1: - exap2 = exap + .5*exap - dev = exap2/3. - else: - exap2 = exap + .2*exap - dev = exap2/4. - while j < len(pos): - if pos[j]-exap2 < 0: - x = ejx[:int(pos[j]+exap2+1)] - y = d[:int(pos[j]+exap2+1)] - elif pos[j]+exap2+1 > len(d): - x = ejx[int(pos[j]-exap2):] - y = d[int(pos[j]-exap2):] - else: - x = ejx[int(pos[j]-exap2):int(pos[j]+exap2+1)] - y = d[int(pos[j]-exap2):int(pos[j]+exap2+1)] - - if mode == 1: - if len(x) < 4: - tref.append(ref[j]) - else: - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) + y.min() - ty2 = np.zeros(len(tx2)) + y.min() - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) - tref.append(p[2]) - else: - if len(x) < 7: - tref.append(ref[j]) - else: - tx1 = np.arange(x[0]-dev,x[0],1) - tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) - ty1 = np.zeros(len(tx1)) + y.min() - ty2 = np.zeros(len(tx2)) + y.min() - x = np.hstack((tx1,x,tx2)) - y = np.hstack((ty1,y,ty2)) - y -= y.min() - midi = int(0.5*len(x)) - guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] - p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) - tref.append(0.5*(p[2]+p[3])) - - j+=1 - - oref = ref.copy() - tref = np.array(tref) - dif = tref-ref - - coef = np.polyfit(ref,dif,nc2) - - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - dif = np.delete(dif,im) - ref = np.delete(ref,im) - coef = np.polyfit(ref,dif,nc2) - residuals = dif - np.polyval(coef,ref) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - - cdif = np.polyval(coef,oref) - ref = oref + cdif - #plot(ref,np.polyval(coef,ref)) - #show() - mat[:,i] = ref - i+=4 + #print i + d = sc[:,i] + j = 0 + pos = np.around(ref).astype('int') + tref = [] + if mode == 1 or mode == 2: + if mode == 1: + exap2 = exap + .5*exap + dev = exap2/3. + else: + exap2 = exap + .2*exap + dev = exap2/4. + while j < len(pos): + if pos[j]-exap2 < 0: + x = ejx[:int(pos[j]+exap2+1)] + y = d[:int(pos[j]+exap2+1)] + elif pos[j]+exap2+1 > len(d): + x = ejx[int(pos[j]-exap2):] + y = d[int(pos[j]-exap2):] + else: + x = ejx[int(pos[j]-exap2):int(pos[j]+exap2+1)] + y = d[int(pos[j]-exap2):int(pos[j]+exap2+1)] + + if mode == 1: + if len(x) < 4: + tref.append(ref[j]) + else: + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + y.min() + ty2 = np.zeros(len(tx2)) + y.min() + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + p, success = scipy.optimize.leastsq(errfunc, [y.min(),y.max()-y.min(),x.mean(),dev], args=(y,x)) + tref.append(p[2]) + else: + if len(x) < 7: + tref.append(ref[j]) + else: + tx1 = np.arange(x[0]-dev,x[0],1) + tx2 = np.arange(x[-1]+1,x[-1]+dev+1,1) + ty1 = np.zeros(len(tx1)) + y.min() + ty2 = np.zeros(len(tx2)) + y.min() + x = np.hstack((tx1,x,tx2)) + y = np.hstack((ty1,y,ty2)) + y -= y.min() + midi = int(0.5*len(x)) + guess = [np.max(y[:midi]),np.max(y[midi:]),x[0]+np.argmax(y[:midi]),x[0]+midi+np.argmax(y[midi:]),dev,dev] + p, success = scipy.optimize.leastsq(res_gauss2, guess, args=(y,x)) + tref.append(0.5*(p[2]+p[3])) + + j+=1 + + oref = ref.copy() + tref = np.array(tref) + dif = tref-ref + + coef = np.polyfit(ref,dif,nc2) + + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + dif = np.delete(dif,im) + ref = np.delete(ref,im) + coef = np.polyfit(ref,dif,nc2) + residuals = dif - np.polyval(coef,ref) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + + cdif = np.polyval(coef,oref) + ref = oref + cdif + #plot(ref,np.polyval(coef,ref)) + #show() + mat[:,i] = ref + i+=4 #imshow(sc_or,vmax=5000) for i in range(mat.shape[0]): - y = mat[i] - x = np.arange(len(y)) - I = np.where(y!=0)[0] - x,y = x[I],y[I]+startfrom - coef = np.polyfit(x,y,ncoef) - #plot(x,y,'bo') - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - cond = True - if len(I)==0: - cond = False - while cond: - im = np.argmax(np.absolute(residuals)) - x = np.delete(x,im) - y = np.delete(y,im) - coef = np.polyfit(x,y,ncoef) - residuals = y - np.polyval(coef,x) - rms = np.sqrt(np.var(residuals)) - I = np.where(np.absolute(residuals)>3*rms)[0] - if len(I)==0: - cond = False - #coef[-1] += startfrom - if i == 0: - acoefs = coef - else: - acoefs = np.vstack((acoefs,coef)) - #plot(np.polyval(coef,np.arange(len(mat[i]))),'k') + y = mat[i] + x = np.arange(len(y)) + I = np.where(y!=0)[0] + x,y = x[I],y[I]+startfrom + coef = np.polyfit(x,y,ncoef) + #plot(x,y,'bo') + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + cond = True + if len(I)==0: + cond = False + while cond: + im = np.argmax(np.absolute(residuals)) + x = np.delete(x,im) + y = np.delete(y,im) + coef = np.polyfit(x,y,ncoef) + residuals = y - np.polyval(coef,x) + rms = np.sqrt(np.var(residuals)) + I = np.where(np.absolute(residuals)>3*rms)[0] + if len(I)==0: + cond = False + #coef[-1] += startfrom + if i == 0: + acoefs = coef + else: + acoefs = np.vstack((acoefs,coef)) + #plot(np.polyval(coef,np.arange(len(mat[i]))),'k') #show() #print gfds @@ -553,48 +557,48 @@ def res_gauss2(params,g,x): return acoefs, len(acoefs) def good_orders(coef,nord,ny,nx,ext_aperture): - Centers = np.zeros((nord,nx)) - ejx = np.arange(nx) - bad_inx = [] - for i in range(nord): - Centers[i,:]=scipy.polyval(coef[i],ejx) - I = np.where(Centers[i,:]+ext_aperture>ny)[0] - if len(I)>0: - bad_inx.append(i) - bad_inx = np.array(bad_inx) - if len(bad_inx) > 0: - im = np.min(bad_inx) - return coef[:im], nord-len(bad_inx) - else: - return coef, nord + Centers = np.zeros((nord,nx)) + ejx = np.arange(nx) + bad_inx = [] + for i in range(nord): + Centers[i,:]=scipy.polyval(coef[i],ejx) + I = np.where(Centers[i,:]+ext_aperture>ny)[0] + if len(I)>0: + bad_inx.append(i) + bad_inx = np.array(bad_inx) + if len(bad_inx) > 0: + im = np.min(bad_inx) + return coef[:im], nord-len(bad_inx) + else: + return coef, nord def get_zero_order_number(ords,wavs): - ords,wvas = np.array(ords),np.array(wavs) - i = 0 - o0 = 0 - pends = [] - o0s = np.arange(200) - figure(1) - subplot(211) - for i in o0s: - orders = ords + i - val = orders*wavs - val2 = val/np.add.reduce(val) - coef = np.polyfit(ords,val2,1) - print coef - pends.append(coef[0]) - plot(ords,val2) - pends = np.array(pends) - I = np.argmin(pends**2) - print o0s[I] - xlabel('raw order number') - ylabel(' $\lambda_c$*(order number + zero order)') - subplot(212) - plot(o0s,np.absolute(pends)) - xlabel('zero order') - ylabel('slope') - show() - print gfdsa + ords,wvas = np.array(ords),np.array(wavs) + i = 0 + o0 = 0 + pends = [] + o0s = np.arange(200) + figure(1) + subplot(211) + for i in o0s: + orders = ords + i + val = orders*wavs + val2 = val/np.add.reduce(val) + coef = np.polyfit(ords,val2,1) + print(coef) + pends.append(coef[0]) + plot(ords,val2) + pends = np.array(pends) + I = np.argmin(pends**2) + print(o0s[I]) + xlabel('raw order number') + ylabel(' $\lambda_c$*(order number + zero order)') + subplot(212) + plot(o0s,np.absolute(pends)) + xlabel('zero order') + ylabel('slope') + show() + print(gfdsa) def Mesh(ycpoly_ob,ycpoly_co): """ @@ -606,332 +610,332 @@ def Mesh(ycpoly_ob,ycpoly_co): for i in range(len(I)): if i == 0: npoly = ycpoly[I[i]] - else: - npoly = np.vstack((npoly,ycpoly[I[i]])) + else: + npoly = np.vstack((npoly,ycpoly[I[i]])) return npoly def get_mask(sc,coefs,spa): - for i in range(len(coefs)): - vec = np.polyval(coefs[i],np.arange(sc.shape[1])).astype('int') - if i == 0: - traces = vec.copy() - else: - traces = np.vstack((traces,vec)) - - mask = np.zeros(sc.shape) - for i in range(traces.shape[1]): - for j in range(traces.shape[0]): - mask[traces[j,i]-spa:traces[j,i]+spa+1,i] = 1 - return mask + for i in range(len(coefs)): + vec = np.polyval(coefs[i],np.arange(sc.shape[1])).astype('int') + if i == 0: + traces = vec.copy() + else: + traces = np.vstack((traces,vec)) + + mask = np.zeros(sc.shape) + for i in range(traces.shape[1]): + for j in range(traces.shape[0]): + mask[traces[j,i]-spa:traces[j,i]+spa+1,i] = 1 + return mask def get_scat(sc,lim,span = 7, typ='median', allow_neg=False,option=0): - scat = np.zeros(sc.shape) - ejeX = np.arange(sc.shape[0]) - - for y in range(sc.shape[1]): - lims = np.around(lim[:,y]).astype('int') - nejX,nejY = np.array([]),np.array([]) - #plot(sc[:,y]) - for j in range(len(lims)): - if j == 0: - #print lims[j] - span - if lims[j] - span < 0: - ejx, ejy = [],[] - elif lims[j] - 2 * span < 0: - ejx=ejeX[:lims[j]-span] - ejy=sc[:lims[j]-span,y] - else: - ejx=ejeX[lims[j]- 2 * span:lims[j]-span+1] - ejy=sc[lims[j]- 2 * span:lims[j]-span+1,y] - - - else: - if lims[j-1] + span >= sc.shape[0] or lims[j-1] + span < 0: - ejx,ejy = [],[] - - elif lims[j] - span + 1 > sc.shape[0]: - ejx=ejeX[lims[j-1] + span: ] - ejy=sc[lims[j-1] + span:, y] - elif lims[j-1] + span >= lims[j]- span + 1: - ejx,ejy = [],[] - else: - ejx=ejeX[lims[j-1] + span:lims[j]- span + 1 ] - ejy=sc[lims[j-1] + span:lims[j]- span + 1, y] - - if option == 1 and len(ejx) == 0: - tpos = int(np.around(0.5*(lims[j-1] + lims[j]))) - if tpos >= 0 and tpos < sc.shape[0]: - ejx = np.array([ejeX[tpos]]) - ejy = np.array([sc[tpos,y]]) - - #plot(sc[:,y]) - #plot(lims[j],sc[lims[j],y],'ro') - #plot(ejx,ejy) - #show() - #print fd - - if len(ejy)>0: - if typ== 'median': - value = np.median(ejy) - elif typ == 'min': - value = np.min(ejy) - - if np.isnan(value)==True or np.isnan(-value)==True: - value = 0. - if value < 0 and (not allow_neg): - value = 0. - - if len(ejx) > 0: - if len(nejX) == 0: - nejX = np.hstack((nejX,np.median(ejx))) - nejY = np.hstack((nejY,value)) - elif np.median(ejx) > nejX[-1]: - nejX = np.hstack((nejX,np.median(ejx))) - nejY = np.hstack((nejY,value)) - if j == 1 and len(nejY)>1: - nejY[0] = nejY[1] - - if lims[-1]+span >= sc.shape[0]: - ejx,ejy = [],[] - elif lims[-1]+2*span > sc.shape[0]: - ejx,ejy = ejeX[lims[-1]+span:],sc[lims[-1]+span:,y] - else: - ejx=ejeX[lims[-1]+span:lims[-1]+2*span] - ejy=sc[lims[-1]+span:lims[-1]+2*span,y] - - if len(ejx)>0: - value = np.median(ejy) - if value < 0 or np.isnan(value)==True or np.isnan(-value)==True: - value = 0. - nejX = np.hstack((nejX,np.median(ejx))) - nejY = np.hstack((nejY,value)) - tck = interpolate.splrep(nejX,nejY,k=1) - - scat[:lims[-1]+2*span,y] = interpolate.splev(ejeX,tck)[:lims[-1]+2*span] - - #plot(np.arange(sc.shape[0])[lim[:,1000].astype('int')],sc[lim[:,1000].astype('int'),1000],'ro') - #plot(sc[:,1000]) - #plot(scat[:,1000]) - #show() - #plot(scat[1000]) - scat = scipy.signal.medfilt(scat,[5,15]) - #plot(scat[1000]) - #show() - return scat + scat = np.zeros(sc.shape) + ejeX = np.arange(sc.shape[0]) + + for y in range(sc.shape[1]): + lims = np.around(lim[:,y]).astype('int') + nejX,nejY = np.array([]),np.array([]) + #plot(sc[:,y]) + for j in range(len(lims)): + if j == 0: + #print lims[j] - span + if lims[j] - span < 0: + ejx, ejy = [],[] + elif lims[j] - 2 * span < 0: + ejx=ejeX[:lims[j]-span] + ejy=sc[:lims[j]-span,y] + else: + ejx=ejeX[lims[j]- 2 * span:lims[j]-span+1] + ejy=sc[lims[j]- 2 * span:lims[j]-span+1,y] + + + else: + if lims[j-1] + span >= sc.shape[0] or lims[j-1] + span < 0: + ejx,ejy = [],[] + + elif lims[j] - span + 1 > sc.shape[0]: + ejx=ejeX[lims[j-1] + span: ] + ejy=sc[lims[j-1] + span:, y] + elif lims[j-1] + span >= lims[j]- span + 1: + ejx,ejy = [],[] + else: + ejx=ejeX[lims[j-1] + span:lims[j]- span + 1 ] + ejy=sc[lims[j-1] + span:lims[j]- span + 1, y] + + if option == 1 and len(ejx) == 0: + tpos = int(np.around(0.5*(lims[j-1] + lims[j]))) + if tpos >= 0 and tpos < sc.shape[0]: + ejx = np.array([ejeX[tpos]]) + ejy = np.array([sc[tpos,y]]) + + #plot(sc[:,y]) + #plot(lims[j],sc[lims[j],y],'ro') + #plot(ejx,ejy) + #show() + #print fd + + if len(ejy)>0: + if typ== 'median': + value = np.median(ejy) + elif typ == 'min': + value = np.min(ejy) + + if np.isnan(value)==True or np.isnan(-value)==True: + value = 0. + if value < 0 and (not allow_neg): + value = 0. + + if len(ejx) > 0: + if len(nejX) == 0: + nejX = np.hstack((nejX,np.median(ejx))) + nejY = np.hstack((nejY,value)) + elif np.median(ejx) > nejX[-1]: + nejX = np.hstack((nejX,np.median(ejx))) + nejY = np.hstack((nejY,value)) + if j == 1 and len(nejY)>1: + nejY[0] = nejY[1] + + if lims[-1]+span >= sc.shape[0]: + ejx,ejy = [],[] + elif lims[-1]+2*span > sc.shape[0]: + ejx,ejy = ejeX[lims[-1]+span:],sc[lims[-1]+span:,y] + else: + ejx=ejeX[lims[-1]+span:lims[-1]+2*span] + ejy=sc[lims[-1]+span:lims[-1]+2*span,y] + + if len(ejx)>0: + value = np.median(ejy) + if value < 0 or np.isnan(value)==True or np.isnan(-value)==True: + value = 0. + nejX = np.hstack((nejX,np.median(ejx))) + nejY = np.hstack((nejY,value)) + tck = interpolate.splrep(nejX,nejY,k=1) + + scat[:lims[-1]+2*span,y] = interpolate.splev(ejeX,tck)[:lims[-1]+2*span] + + #plot(np.arange(sc.shape[0])[lim[:,1000].astype('int')],sc[lim[:,1000].astype('int'),1000],'ro') + #plot(sc[:,1000]) + #plot(scat[:,1000]) + #show() + #plot(scat[1000]) + scat = scipy.signal.medfilt(scat,[5,15]) + #plot(scat[1000]) + #show() + return scat def get_scat2(sc,ps): - ejex = np.arange(sc.shape[0]) - bac = sc.copy() - #print sc.shape - #print ps.shape - for col in range(sc.shape[1]): - #print col - #col = 2000 - J = np.where(ps[:,col] == 0)[0] - Z = np.zeros((sc.shape[0])) - Z[J] = 1. - #plot(sc[:,col]) - #plot(sc[:,col]*Z,linewidth=2.0) - #show() - #print gfd - Z2 = np.append(Z[-1],Z[:-1]) - I = np.where((Z!=0) & (Z2==0))[0] - J = np.where((Z2!=0) & (Z==0))[0] - J = J[1:] - I = I[:-1] - points = [] - vals = [] - for i in range(len(I)): - #plt.plot(np.mean(ejex[I[i]:J[i]]),np.median(sc[I[i]:J[i],col]),'ro') - points.append(np.mean(ejex[I[i]:J[i]])) - vals.append(np.median(sc[I[i]:J[i],col])) - #plt.show() - - if len(points)>2: - points,vals = np.array(points),np.array(vals) - F = np.where(vals < 10000)[0] - vals = vals[F] - points = points[F] - tck = interpolate.splrep(points,vals,k=1) - scat = interpolate.splev(ejex,tck) - scat[:I[0]] = scat[I[0]] - scat[J[-1]:] = scat[J[-1]] - bac[:,col] = scat - #plot(ejex,scat,'r') - #show() - else: - bac[:,col] = 0. - - bacm = signal.medfilt2d(bac,[1,21]) - #plot(bac[1000]) - #plot(bacm[1000]) - #show() - return bacm + ejex = np.arange(sc.shape[0]) + bac = sc.copy() + #print sc.shape + #print ps.shape + for col in range(sc.shape[1]): + #print col + #col = 2000 + J = np.where(ps[:,col] == 0)[0] + Z = np.zeros((sc.shape[0])) + Z[J] = 1. + #plot(sc[:,col]) + #plot(sc[:,col]*Z,linewidth=2.0) + #show() + #print gfd + Z2 = np.append(Z[-1],Z[:-1]) + I = np.where((Z!=0) & (Z2==0))[0] + J = np.where((Z2!=0) & (Z==0))[0] + J = J[1:] + I = I[:-1] + points = [] + vals = [] + for i in range(len(I)): + #plt.plot(np.mean(ejex[I[i]:J[i]]),np.median(sc[I[i]:J[i],col]),'ro') + points.append(np.mean(ejex[I[i]:J[i]])) + vals.append(np.median(sc[I[i]:J[i],col])) + #plt.show() + + if len(points)>2: + points,vals = np.array(points),np.array(vals) + F = np.where(vals < 10000)[0] + vals = vals[F] + points = points[F] + tck = interpolate.splrep(points,vals,k=1) + scat = interpolate.splev(ejex,tck) + scat[:I[0]] = scat[I[0]] + scat[J[-1]:] = scat[J[-1]] + bac[:,col] = scat + #plot(ejex,scat,'r') + #show() + else: + bac[:,col] = 0. + + bacm = signal.medfilt2d(bac,[1,21]) + #plot(bac[1000]) + #plot(bacm[1000]) + #show() + return bacm def clean(x,y): - lI = len(x) - coef = np.polyfit(x,y,1) - res = y - np.polyval(coef,x) - dev = np.sqrt( np.sum(res**2) / float(len(res) - 2) ) - I = np.where(np.abs(res) < 2*dev)[0] - cond = True - if len(I) == lI: - cond = False - - while cond: - iw = np.argmax(res**2) - x = np.delete(x,iw) - y = np.delete(y,iw) - coef = np.polyfit(x,y,1) - res = y - np.polyval(coef,x) - dev = np.sqrt( np.sum(res**2) / float(len(res) - 2) ) - #plot(x,y,'.') - #plot(x,np.polyval(coef,x)) - #plot(x,np.polyval(coef,x)+2*dev) - #plot(x,np.polyval(coef,x)-2*dev) - #show() - I = np.where(np.abs(res) < 2*dev)[0] - J = np.where(np.abs(res) >= 2*dev)[0] - if len(J) == 0: - cond = False - return x,y + lI = len(x) + coef = np.polyfit(x,y,1) + res = y - np.polyval(coef,x) + dev = np.sqrt( np.sum(res**2) / float(len(res) - 2) ) + I = np.where(np.abs(res) < 2*dev)[0] + cond = True + if len(I) == lI: + cond = False + + while cond: + iw = np.argmax(res**2) + x = np.delete(x,iw) + y = np.delete(y,iw) + coef = np.polyfit(x,y,1) + res = y - np.polyval(coef,x) + dev = np.sqrt( np.sum(res**2) / float(len(res) - 2) ) + #plot(x,y,'.') + #plot(x,np.polyval(coef,x)) + #plot(x,np.polyval(coef,x)+2*dev) + #plot(x,np.polyval(coef,x)-2*dev) + #show() + I = np.where(np.abs(res) < 2*dev)[0] + J = np.where(np.abs(res) >= 2*dev)[0] + if len(J) == 0: + cond = False + return x,y def sig_cli(v,ns=2.): - vm = np.median(v) - res = v - vm - dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) - I = np.where(np.abs(res)>ns*dev)[0] - cond = True - if len(I)==0: - cond = False - while cond: - iw = np.argmax(res**2) - v = np.delete(v,iw) - vm = np.median(v) - res = v - vm - dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) - I = np.where(np.absolute(res)>ns*dev)[0] - if len(I)==0: - cond = False - return vm,dev + vm = np.median(v) + res = v - vm + dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) + I = np.where(np.abs(res)>ns*dev)[0] + cond = True + if len(I)==0: + cond = False + while cond: + iw = np.argmax(res**2) + v = np.delete(v,iw) + vm = np.median(v) + res = v - vm + dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) + I = np.where(np.absolute(res)>ns*dev)[0] + if len(I)==0: + cond = False + return vm,dev def sig_cli2(v,ns=3.): - vm = np.median(v) - res = v - vm - dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) - I = np.where(np.abs(res)>ns*dev)[0] - cond = True - if len(I)==0: - cond = False - while cond: - iw = np.argmax(res**2) - v = np.delete(v,iw) - vm = np.median(v) - res = v - vm - dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) - I = np.where(np.absolute(res)>ns*dev)[0] - if len(I)==0: - cond = False - return v + vm = np.median(v) + res = v - vm + dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) + I = np.where(np.abs(res)>ns*dev)[0] + cond = True + if len(I)==0: + cond = False + while cond: + iw = np.argmax(res**2) + v = np.delete(v,iw) + vm = np.median(v) + res = v - vm + dev = np.sqrt(np.sum(res**2) / float(len(res)-1)) + I = np.where(np.absolute(res)>ns*dev)[0] + if len(I)==0: + cond = False + return v def fit(x,y,n): - xo = x.copy() - coef = np.polyfit(x,y,n) - res = y - np.polyval(coef,x) - dev = np.sqrt(np.sum(res**2)/float(len(x)-n-1)) - I = np.where(np.absolute(res)>3*dev)[0] - cond = True - if len(I) == 0: - cond = False - while cond: - iw = np.argmax(res**2) - x = np.delete(x,iw) - y = np.delete(y,iw) - coef = np.polyfit(x,y,n) - res = y - np.polyval(coef,x) - dev = np.sqrt(np.sum(res**2)/float(len(x)-n-1)) - I = np.where(np.absolute(res)>3*dev)[0] - if len(I) == 0: - cond = False - return np.around(np.polyval(coef,xo)).astype('int') - + xo = x.copy() + coef = np.polyfit(x,y,n) + res = y - np.polyval(coef,x) + dev = np.sqrt(np.sum(res**2)/float(len(x)-n-1)) + I = np.where(np.absolute(res)>3*dev)[0] + cond = True + if len(I) == 0: + cond = False + while cond: + iw = np.argmax(res**2) + x = np.delete(x,iw) + y = np.delete(y,iw) + coef = np.polyfit(x,y,n) + res = y - np.polyval(coef,x) + dev = np.sqrt(np.sum(res**2)/float(len(x)-n-1)) + I = np.where(np.absolute(res)>3*dev)[0] + if len(I) == 0: + cond = False + return np.around(np.polyval(coef,xo)).astype('int') + def bkg_flat(data,lim,span = 7): - bac = data.copy() - ejx = np.arange(data.shape[0]) - for i in range(data.shape[1]): - #print i - #i = 4098 - line = data[:,i] - #plot(line) - #show() - tline = line.copy() - lims = lim[:,i].astype('int') - dys = [] - L1s,L2s=[],[] - for j in range(len(lims)): - #print j - x = ejx[lims[j]-span:lims[j]+span+1] - y = line[lims[j]-span:lims[j]+span+1] - my,dy = sig_cli(y) - #dy = np.sqrt(np.var(y)) - I = np.where(line[:lims[j]] < my-3*dy)[0] - if len(I)==0: - L1 = 0 - else: - L1 = I[-1] - I = np.where(line[lims[j]:] < my-3*dy)[0] - if len(I)==0: - L2 = 0 - else: - L2 = lims[j]+I[0] - L1s.append(L1) - L2s.append(L2) - - #if j == 0: - # tline[:L1 - span] = 0 - #if j == len(lims)-1: - # tline[L2+span:] = 0 - #print L1,L2 - #plot(ejx[L1:L2+1],line[L1:L2+1]) - #dys.append(my) - #plot(x,y,'r') - #plot(x,np.zeros(len(x))+ my + 3*dy,'k') - #plot(x,np.zeros(len(x))+ my - 3*dy,'k') - #show() - L1s,L2s = np.array(L1s),np.array(L2s) - ref = np.arange(len(L1s)) - #print L1s,L2s - L1s = fit(ref,L1s,3) - L2s = fit(ref,L2s,3) - #print L1s,L2s - for j in range(len(lims)): - tline[L1s[j]:L2s[j]+1] = 0 - tline[:L1s[0] - span] = 0 - tline[L2s[-1] + span:] = 0 - I = np.where(tline!=0)[0] - tx = ejx[I] - ty = tline[I] - tx2 = np.append(tx[-1],tx[:-1]) - I = np.where(tx2 +1 != tx )[0] - vx,vy = [],[] - for j in range(len(I)): - if j != len(I)-1: - vy.append(np.median(ty[I[j]:I[j+1]])) - vx.append(np.median(tx[I[j]:I[j+1]])) - else: - vy.append(np.median(ty[I[j]:])) - vx.append(np.median(tx[I[j]:])) - tck = interpolate.splrep(vx,vy,k=1) - bkg = interpolate.splev(ejx,tck) - bkg[:L1s[0] - span] = bkg[L1s[0] - span] - bkg[L2s[-1] + span:] = bkg[L2s[-1] + span] - bac[:,i] = bkg - #plot(data[:,i]) - #plot(bac[:,i]) - #show() - span1 = (span/2)*2 + 1 - bac = signal.medfilt(bac,[span1,span*2+1]) - return bac + bac = data.copy() + ejx = np.arange(data.shape[0]) + for i in range(data.shape[1]): + #print i + #i = 4098 + line = data[:,i] + #plot(line) + #show() + tline = line.copy() + lims = lim[:,i].astype('int') + dys = [] + L1s,L2s=[],[] + for j in range(len(lims)): + #print j + x = ejx[lims[j]-span:lims[j]+span+1] + y = line[lims[j]-span:lims[j]+span+1] + my,dy = sig_cli(y) + #dy = np.sqrt(np.var(y)) + I = np.where(line[:lims[j]] < my-3*dy)[0] + if len(I)==0: + L1 = 0 + else: + L1 = I[-1] + I = np.where(line[lims[j]:] < my-3*dy)[0] + if len(I)==0: + L2 = 0 + else: + L2 = lims[j]+I[0] + L1s.append(L1) + L2s.append(L2) + + #if j == 0: + # tline[:L1 - span] = 0 + #if j == len(lims)-1: + # tline[L2+span:] = 0 + #print L1,L2 + #plot(ejx[L1:L2+1],line[L1:L2+1]) + #dys.append(my) + #plot(x,y,'r') + #plot(x,np.zeros(len(x))+ my + 3*dy,'k') + #plot(x,np.zeros(len(x))+ my - 3*dy,'k') + #show() + L1s,L2s = np.array(L1s),np.array(L2s) + ref = np.arange(len(L1s)) + #print L1s,L2s + L1s = fit(ref,L1s,3) + L2s = fit(ref,L2s,3) + #print L1s,L2s + for j in range(len(lims)): + tline[L1s[j]:L2s[j]+1] = 0 + tline[:L1s[0] - span] = 0 + tline[L2s[-1] + span:] = 0 + I = np.where(tline!=0)[0] + tx = ejx[I] + ty = tline[I] + tx2 = np.append(tx[-1],tx[:-1]) + I = np.where(tx2 +1 != tx )[0] + vx,vy = [],[] + for j in range(len(I)): + if j != len(I)-1: + vy.append(np.median(ty[I[j]:I[j+1]])) + vx.append(np.median(tx[I[j]:I[j+1]])) + else: + vy.append(np.median(ty[I[j]:])) + vx.append(np.median(tx[I[j]:])) + tck = interpolate.splrep(vx,vy,k=1) + bkg = interpolate.splev(ejx,tck) + bkg[:L1s[0] - span] = bkg[L1s[0] - span] + bkg[L2s[-1] + span:] = bkg[L2s[-1] + span] + bac[:,i] = bkg + #plot(data[:,i]) + #plot(bac[:,i]) + #show() + span1 = (span/2)*2 + 1 + bac = signal.medfilt(bac,[span1,span*2+1]) + return bac def MedianCombine(ImgList,ZF=0.): """ @@ -947,7 +951,7 @@ def MedianCombine(ImgList,ZF=0.): h = pyfits.open(ImgList[0])[0] d = h.data d = OverscanTrim(d) - ZP - + factor = 1.25 if (n < 3): factor = 1 @@ -976,7 +980,7 @@ def MedianCombine_simple(ImgList,ZF=0.): h = pyfits.open(ImgList[0])[0] d = h.data - ZF - + factor = 1.25 if (n < 3): factor = 1 @@ -989,10 +993,10 @@ def MedianCombine_simple(ImgList,ZF=0.): else: for i in range(n-1): h = pyfits.open(ImgList[i+1])[0] - dtemp = h.data-ZF - #plot(dtemp[:,1000]) + dtemp = h.data-ZF + #plot(dtemp[:,1000]) d = np.dstack((d,h.data-ZF)) - #show() + #show() return np.median(d,axis=2) @@ -1008,7 +1012,7 @@ def FlatNormalize(S_flat_ob, S_flat_ob_simple, mid=1023): for j in np.arange(norders).astype('int'): max_val = (scipy.signal.medfilt(S_flat_ob[j,1,int(np.around(mid-span)):int(np.around(mid+span+1))],21)).max() S_flat_ob_n[j,1,:] = (S_flat_ob[j,1,:] / max_val) - S_flat_ob_n[j,2,:] = S_flat_ob[j,1,:] * max_val**2 + S_flat_ob_n[j,2,:] = S_flat_ob[j,1,:] * max_val**2 max_val = (scipy.signal.medfilt(S_flat_ob_simple[j,int(np.around(mid-span)):int(np.around(mid+span+1))],21)).max() S_flat_ob_simple_n[j,:] = (S_flat_ob_simple[j,:] / max_val) @@ -1040,228 +1044,228 @@ def FlatNormalize_single(S_flat,mid=1023,span=500): return S_flat_n,np.array(max_vals) def retrace(dat, c_all,span=9): - def gauss(params,x): - med = params[0] - sig = params[1] - g = np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) - return g - - def res_gauss(params,g,x): - return g-gauss(params,x) - - #for i in range(len(c_all)): - # px = int(np.around(np.polyval(c_all[i],2000))) - # plot(dat[px-7:px+8,2000]) - #show() - - med = int(.5*dat.shape[1]) - #print dat.shape - Centers = np.zeros((len(c_all),dat.shape[1])) - Cen = np.zeros((len(c_all),dat.shape[1])) - for i in range(c_all.shape[0]): - Centers[i,:] = np.around(scipy.polyval(c_all[i,:],np.arange(dat.shape[1]))) - Cen [i,:] = scipy.polyval(c_all[i,:],np.arange(dat.shape[1])) - i = -span - CCF = [] - pix = [] - #plot(dat[:,1000]) - #show() - while i < span+1: - - mat = np.zeros(dat.shape) - for j in range(dat.shape[1]): - vec = Centers[:,j] - vec = vec.astype('int')+i - IN = np.where((vec>0) & (vec 0: - if CCF[jj-1] >= CCF[jj] and CCF[jj+1] >=CCF[jj]: - mi = jj - break - jj-=1 - jj = im - ma = len(CCF)-1 - while jj < len(CCF)-1: - - if CCF[jj-1] >= CCF[jj] and CCF[jj+1]>= CCF[jj]: - ma = jj - break - jj+=1 - - if pix[im]<0: - guess = [pix[im],3.0] - else: - guess = [pix[im],3.0] - #plot(pix,CCF) - #show() - Gfit = optimize.leastsq(res_gauss,guess,args=(CCF[mi:ma+1],pix[mi:ma+1])) - shift = Gfit[0][0] - - #print 'yshift:', shift - - c_new = c_all.copy() - for j in range(c_all.shape[0]): - c_new[j] = np.polyfit( np.arange(dat.shape[1]), Cen[j]+shift, c_all.shape[1]-1 ) - - return c_new,int(round(shift)) + def gauss(params,x): + med = params[0] + sig = params[1] + g = np.exp(-0.5*(x-med)*(x-med)/(sig*sig)) + return g + + def res_gauss(params,g,x): + return g-gauss(params,x) + + #for i in range(len(c_all)): + # px = int(np.around(np.polyval(c_all[i],2000))) + # plot(dat[px-7:px+8,2000]) + #show() + + med = int(.5*dat.shape[1]) + #print dat.shape + Centers = np.zeros((len(c_all),dat.shape[1])) + Cen = np.zeros((len(c_all),dat.shape[1])) + for i in range(c_all.shape[0]): + Centers[i,:] = np.around(scipy.polyval(c_all[i,:],np.arange(dat.shape[1]))) + Cen [i,:] = scipy.polyval(c_all[i,:],np.arange(dat.shape[1])) + i = -span + CCF = [] + pix = [] + #plot(dat[:,1000]) + #show() + while i < span+1: + + mat = np.zeros(dat.shape) + for j in range(dat.shape[1]): + vec = Centers[:,j] + vec = vec.astype('int')+i + IN = np.where((vec>0) & (vec 0: + if CCF[jj-1] >= CCF[jj] and CCF[jj+1] >=CCF[jj]: + mi = jj + break + jj-=1 + jj = im + ma = len(CCF)-1 + while jj < len(CCF)-1: + + if CCF[jj-1] >= CCF[jj] and CCF[jj+1]>= CCF[jj]: + ma = jj + break + jj+=1 + + if pix[im]<0: + guess = [pix[im],3.0] + else: + guess = [pix[im],3.0] + #plot(pix,CCF) + #show() + Gfit = optimize.leastsq(res_gauss,guess,args=(CCF[mi:ma+1],pix[mi:ma+1])) + shift = Gfit[0][0] + + #print 'yshift:', shift + + c_new = c_all.copy() + for j in range(c_all.shape[0]): + c_new[j] = np.polyfit( np.arange(dat.shape[1]), Cen[j]+shift, c_all.shape[1]-1 ) + + return c_new,int(round(shift)) def get_drift(data,P,c_all,pii,win=10): - d = np.median(data[:,pii-win:pii+win+1],axis=1) - p = np.median(P[:,pii-win:pii+win+1],axis=1) - d -= d.min() - - for i in range(len(c_all)): - j = int(np.around(np.polyval(c_all[i],pii))) - p[j-win:j+win+1] /= p[j-win:j+win+1].max() - p[j-win:j+win+1] *= d[j-win:j+win+1].max() - ejxo = np.arange(len(p)) - ps = -4 - dels,ccf = [],[] - while ps <=4: - ejxt = ejxo + ps - tck = scipy.interpolate.splrep(ejxt,p,k=3) - tp = scipy.interpolate.splev(ejxo,tck) - ccf.append(np.add.reduce(d[50:-100]*tp[50:-100])) - dels.append(ps) - ps+=0.1 - dels,ccf = np.array(dels),np.array(ccf) - tck = scipy.interpolate.splrep(dels,ccf,k=3) - ndels = np.arange(dels[0],dels[-1],0.01) - nccf = scipy.interpolate.splev(ndels,tck) - shift = ndels[np.argmax(nccf)] - - c_new = c_all.copy() - c_new[:,-1] += shift - return shift, c_new + d = np.median(data[:,pii-win:pii+win+1],axis=1) + p = np.median(P[:,pii-win:pii+win+1],axis=1) + d -= d.min() + + for i in range(len(c_all)): + j = int(np.around(np.polyval(c_all[i],pii))) + p[j-win:j+win+1] /= p[j-win:j+win+1].max() + p[j-win:j+win+1] *= d[j-win:j+win+1].max() + ejxo = np.arange(len(p)) + ps = -4 + dels,ccf = [],[] + while ps <=4: + ejxt = ejxo + ps + tck = scipy.interpolate.splrep(ejxt,p,k=3) + tp = scipy.interpolate.splev(ejxo,tck) + ccf.append(np.add.reduce(d[50:-100]*tp[50:-100])) + dels.append(ps) + ps+=0.1 + dels,ccf = np.array(dels),np.array(ccf) + tck = scipy.interpolate.splrep(dels,ccf,k=3) + ndels = np.arange(dels[0],dels[-1],0.01) + nccf = scipy.interpolate.splev(ndels,tck) + shift = ndels[np.argmax(nccf)] + + c_new = c_all.copy() + c_new[:,-1] += shift + return shift, c_new def invert(spec): - if len(spec.shape)==2: - for i in range(spec.shape[0]): - spec[i] = spec[i][::-1] - else: - for i in range(spec.shape[0]): - spec[i,0] = spec[i,0][::-1] - spec[i,1] = spec[i,1][::-1] - spec[i,2] = spec[i,2][::-1] - return spec + if len(spec.shape)==2: + for i in range(spec.shape[0]): + spec[i] = spec[i][::-1] + else: + for i in range(spec.shape[0]): + spec[i,0] = spec[i,0][::-1] + spec[i,1] = spec[i,1][::-1] + spec[i,2] = spec[i,2][::-1] + return spec def shift_P(P,shift,c_new,ap): - nP = np.zeros(P.shape) - for i in range(P.shape[1]): - d = P [:,i] - ej = np.arange(len(d))+shift - ej2 = np.arange(len(d)) - ej, ej2 = ej.astype('float'),ej2.astype('float') - tck = scipy.interpolate.splrep(ej,d,k=3) - - nP[:,i] = scipy.interpolate.splev(ej2,tck) - I = np.where(nP[:,i]< 0)[0] - nP[I,i]=0 - inv = np.array([]) - for j in range(len(c_new)): - pi = np.polyval(c_new[j],i) - pi = int(np.around(pi)) - #print pi - tv = nP[pi - ap:pi+ap+1,i] - tv /= np.sum(tv) - nP[pi - ap:pi+ap+1,i] = tv - inv = np.hstack( (inv,np.arange(pi-ap,pi+ap+1,1)) ) - - inv = inv.astype('int') - I = np.where(inv>P.shape[0]-1)[0] - if len(I)>0: - inv = np.delete(inv,I) - I = np.where(inv<0)[0] - if len(I)>0: - inv = np.delete(inv,I) - niv = np.zeros(P.shape[0]).astype('int') - niv[inv] = 1.0 - nP[:,i] *= niv - return nP + nP = np.zeros(P.shape) + for i in range(P.shape[1]): + d = P [:,i] + ej = np.arange(len(d))+shift + ej2 = np.arange(len(d)) + ej, ej2 = ej.astype('float'),ej2.astype('float') + tck = scipy.interpolate.splrep(ej,d,k=3) + + nP[:,i] = scipy.interpolate.splev(ej2,tck) + I = np.where(nP[:,i]< 0)[0] + nP[I,i]=0 + inv = np.array([]) + for j in range(len(c_new)): + pi = np.polyval(c_new[j],i) + pi = int(np.around(pi)) + #print pi + tv = nP[pi - ap:pi+ap+1,i] + tv /= np.sum(tv) + nP[pi - ap:pi+ap+1,i] = tv + inv = np.hstack( (inv,np.arange(pi-ap,pi+ap+1,1)) ) + + inv = inv.astype('int') + I = np.where(inv>P.shape[0]-1)[0] + if len(I)>0: + inv = np.delete(inv,I) + I = np.where(inv<0)[0] + if len(I)>0: + inv = np.delete(inv,I) + niv = np.zeros(P.shape[0]).astype('int') + niv[inv] = 1.0 + nP[:,i] *= niv + return nP #geographical functions def obspos(longitude,obsradius,R0): - """ - Set the observatory position respect to geocenter in the coordinates(x,y,z)required by jplepem, - x to equator/greenwich intersection, - y 90 degrees east, - z positive to north - """ - obpos = [] - x = obsradius*np.cos( (np.pi / 180.0) * longitude ) - obpos.append(x) - y = obsradius*np.sin( (np.pi / 180.0) * longitude ) - obpos.append(y) - z = R0 - obpos.append(z) - return obpos + """ + Set the observatory position respect to geocenter in the coordinates(x,y,z)required by jplepem, + x to equator/greenwich intersection, + y 90 degrees east, + z positive to north + """ + obpos = [] + x = obsradius*np.cos( (np.pi / 180.0) * longitude ) + obpos.append(x) + y = obsradius*np.sin( (np.pi / 180.0) * longitude ) + obpos.append(y) + z = R0 + obpos.append(z) + return obpos def JPLR0(lat, altitude): - "the corrections due to earth rotation" - "this function returns the velocity in m/s, the projected distance of the observatory in the equator axis and in the earth spin axis and \ - also returns the distance from the rotation axis, and the distance from the equator plane to the observatory position" - "The arguments are latitude,altitude and hour angle at observatory , dec: the declination of the star\ - the variables are: \ - Raxis: the distance from the observatory to the earth axis, and Psid: the period of sidereal day" - lat = Constants.degtorad*lat - e2 = Constants.f*(2.0-Constants.f) - c1 = 1.0 - e2*(2.0 - e2)*np.sin(lat)**2 - c2 = 1.0 - e2*np.sin(lat)**2 - - #radius at 0 elevation - R0 = Constants.Req*np.sqrt(c1/c2) - - #the geocentric latitude - c1 = e2*np.sin(2.0*lat) - c2 = 2.0*c2 - geolat = lat - np.arctan(c1/c2) - #Calculate geocentric radius at altitude of the observatory - GeoR = R0*np.cos(geolat) + altitude*np.cos(lat) - - # the R0 vector is now the distance from the observatory to the declination 0 deg plane - R0 = R0*np.sin(abs(geolat))+altitude*np.sin(lat) - return GeoR,R0 + "the corrections due to earth rotation" + "this function returns the velocity in m/s, the projected distance of the observatory in the equator axis and in the earth spin axis and \ + also returns the distance from the rotation axis, and the distance from the equator plane to the observatory position" + "The arguments are latitude,altitude and hour angle at observatory , dec: the declination of the star\ + the variables are: \ + Raxis: the distance from the observatory to the earth axis, and Psid: the period of sidereal day" + lat = Constants.degtorad*lat + e2 = Constants.f*(2.0-Constants.f) + c1 = 1.0 - e2*(2.0 - e2)*np.sin(lat)**2 + c2 = 1.0 - e2*np.sin(lat)**2 + + #radius at 0 elevation + R0 = Constants.Req*np.sqrt(c1/c2) + + #the geocentric latitude + c1 = e2*np.sin(2.0*lat) + c2 = 2.0*c2 + geolat = lat - np.arctan(c1/c2) + #Calculate geocentric radius at altitude of the observatory + GeoR = R0*np.cos(geolat) + altitude*np.cos(lat) + + # the R0 vector is now the distance from the observatory to the declination 0 deg plane + R0 = R0*np.sin(abs(geolat))+altitude*np.sin(lat) + return GeoR,R0 def JPLiers(path, mjdini, mjdend): - output = open(path+'iers.tab','w') - filename = path+'finals2000A.data' - finaldata = open(filename,'r') - - for line in finaldata: - mj = line[7:15] - if float(mj) >= float(mjdini) and float(mj) <= float(mjdend) and len(line.split()) > 5: - c1 = line[18:27] - c2 = line[37:46] - c3 = line[58:68] - l = ' '+mj+' '+c1+' '+c2+' '+c3+' '+'\n' - output.write(l) - if mj == mjdini+999: print "estoy en el dia D" - if float(mj) > float(mjdend): - break - finaldata.close() - output.close() - pass + output = open(path+'iers.tab','w') + filename = path+'finals2000A.data' + finaldata = open(filename,'r') + + for line in finaldata: + mj = line[7:15] + if float(mj) >= float(mjdini) and float(mj) <= float(mjdend) and len(line.split()) > 5: + c1 = line[18:27] + c2 = line[37:46] + c3 = line[58:68] + l = ' '+mj+' '+c1+' '+c2+' '+c3+' '+'\n' + output.write(l) + if mj == mjdini+999: print("estoy en el dia D") + if float(mj) > float(mjdend): + break + finaldata.close() + output.close() + pass # Extraction Functions def PCoeff(data, trace_coeffs, Aperture, RON, Gain, NSigma, S, N, Marsh_alg,min_col,max_col): @@ -1269,7 +1273,7 @@ def PCoeff(data, trace_coeffs, Aperture, RON, Gain, NSigma, S, N, Marsh_alg,min_ scipy.polyval(trace_coeffs,np.arange(data.shape[1])).astype('double'), \ data.shape[0], data.shape[1], data.shape[1], Aperture, RON, Gain, \ NSigma, S, N, Marsh_alg,min_col,max_col) - FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. FinalMatrix.resize(data.shape[0],data.shape[1]) # And return the array in matrix-form. return FinalMatrix @@ -1288,7 +1292,7 @@ def PCoeff2(pars): scipy.polyval(trace_coeffs,np.arange(GDATA.shape[1])).astype('double'), \ GDATA.shape[0], GDATA.shape[1], GDATA.shape[1], Aperture, RON, Gain, \ NSigma, S, N, Marsh_alg,min_col,max_col) - FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. FinalMatrix.resize(GDATA.shape[0],GDATA.shape[1]) # And return the array in matrix-form. return FinalMatrix @@ -1298,9 +1302,9 @@ def obtain_P(data, trace_coeffs, Aperture, RON, Gain, NSigma, S, N, Marsh_alg,mi npars_paralel = [] if 'int' in str(type(min_col)) or 'float' in str(type(min_col)): - min_col = np.zeros(len(trace_coeffs)) + int(min_col) + min_col = np.zeros(len(trace_coeffs)) + int(min_col) if 'int' in str(type(max_col)) or 'float' in str(type(max_col)): - max_col = np.zeros(len(trace_coeffs)) + int(max_col) + max_col = np.zeros(len(trace_coeffs)) + int(max_col) for i in range(len(trace_coeffs)): npars_paralel.append([trace_coeffs[i,:],Aperture,RON,Gain,NSigma,S,N,Marsh_alg,int(min_col[i]),int(max_col[i])]) @@ -1315,7 +1319,7 @@ def getSpectrum(P,data,trace_coeffs,Aperture,RON,Gain,S,NCosmic, min_col,max_col P.flatten().astype('double'), data.shape[0],\ data.shape[1],data.shape[1],Aperture,RON,\ Gain,S,NCosmic,min_col,max_col) - FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. FinalMatrix.resize(3,size) # And return the array in matrix-form. return FinalMatrix @@ -1333,7 +1337,7 @@ def getSpectrum2(pars): P.flatten().astype('double'), GDATA.shape[0],\ GDATA.shape[1],GDATA.shape[1],Aperture,RON,\ Gain,S,NCosmic,min_col,max_col) - FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. FinalMatrix.resize(3,size) # And return the array in matrix-form. return FinalMatrix @@ -1343,7 +1347,7 @@ def getSimpleSpectrum(data,trace_coeffs,Aperture,min_col,max_col): data.shape[0],data.shape[1],\ data.shape[1],Aperture,min_col,max_col) FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. - return FinalMatrix + return FinalMatrix def getSimpleSpectrum2(pars): trace_coeffs = pars[0] @@ -1355,136 +1359,136 @@ def getSimpleSpectrum2(pars): GDATA.shape[0],GDATA.shape[1],\ GDATA.shape[1],Aperture,min_col,max_col) FinalMatrix = np.asarray(Result) # After the function, we convert our list to a Numpy array. - return FinalMatrix + return FinalMatrix def simple_extraction(data,coefs,ext_aperture,min_extract_col,max_extract_col,npools): - global GDATA - GDATA = data - npars_paralel = [] - if 'int' in str(type(min_extract_col)) or 'float' in str(type(min_extract_col)): - min_extract_col = np.zeros(len(coefs)) + int(min_extract_col) - if 'int' in str(type(max_extract_col)) or 'float' in str(type(max_extract_col)): - max_extract_col = np.zeros(len(coefs)) + int(max_extract_col) - for i in range(len(coefs)): - npars_paralel.append([coefs[i,:],ext_aperture,int(min_extract_col[i]),int(max_extract_col[i])]) - p = Pool(npools) - spec = np.array((p.map(getSimpleSpectrum2, npars_paralel))) - p.terminate() - return spec + global GDATA + GDATA = data + npars_paralel = [] + if 'int' in str(type(min_extract_col)) or 'float' in str(type(min_extract_col)): + min_extract_col = np.zeros(len(coefs)) + int(min_extract_col) + if 'int' in str(type(max_extract_col)) or 'float' in str(type(max_extract_col)): + max_extract_col = np.zeros(len(coefs)) + int(max_extract_col) + for i in range(len(coefs)): + npars_paralel.append([coefs[i,:],ext_aperture,int(min_extract_col[i]),int(max_extract_col[i])]) + p = Pool(npools) + spec = np.array((p.map(getSimpleSpectrum2, npars_paralel))) + p.terminate() + return spec def optimal_extraction(data,Pin,coefs,ext_aperture,RON,GAIN,MARSH,COSMIC,min_extract_col,max_extract_col,npools): - global GDATA,P - P = Pin - GDATA = data - npars_paralel = [] - if 'int' in str(type(min_extract_col)) or 'float' in str(type(min_extract_col)): - min_extract_col = np.zeros(len(coefs)) + int(min_extract_col) - if 'int' in str(type(max_extract_col)) or 'float' in str(type(max_extract_col)): - max_extract_col = np.zeros(len(coefs)) + int(max_extract_col) - - for i in range(len(coefs)): - npars_paralel.append([coefs[i,:],ext_aperture,RON,GAIN,MARSH,COSMIC,int(min_extract_col[i]),int(max_extract_col[i])]) - p = Pool(npools) - spec = np.array((p.map(getSpectrum2, npars_paralel))) - p.terminate() - return spec + global GDATA,P + P = Pin + GDATA = data + npars_paralel = [] + if 'int' in str(type(min_extract_col)) or 'float' in str(type(min_extract_col)): + min_extract_col = np.zeros(len(coefs)) + int(min_extract_col) + if 'int' in str(type(max_extract_col)) or 'float' in str(type(max_extract_col)): + max_extract_col = np.zeros(len(coefs)) + int(max_extract_col) + + for i in range(len(coefs)): + npars_paralel.append([coefs[i,:],ext_aperture,RON,GAIN,MARSH,COSMIC,int(min_extract_col[i]),int(max_extract_col[i])]) + p = Pool(npools) + spec = np.array((p.map(getSpectrum2, npars_paralel))) + p.terminate() + return spec # CCF functions def get_herms(horder): norms = [] herms = [] for l in range(3,horder+1): - norms.append( 1.0 / np.sqrt(scipy.misc.factorial(l) * (2 ** l) ) ) + norms.append( 1.0 / np.sqrt(scipy.misc.factorial(l) * (2 ** l) ) ) herms.append( scipy.special.hermite( l ) ) return norms, herms def XCor(spectra, mask_l, mask_h, mask_w, vel, lbary_ltopo, vel_width=30,\ vel_step=0.3, start_order=0, spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300.): - """ - Calculates the cross-correlation function for a Coralie Spectra - """ - # speed of light, km/s - c = 2.99792458E5 - - # loop over orders - norders = spectra.shape[1] - - # determine minimum velocities - vel_min = vel - vel_width - vel_max = vel + vel_width - N = int(np.ceil( (2*vel_width) / vel_step )) - - Xcor_full = np.zeros( (N, norders+1) ) - sn = np.zeros( (norders) ) - nlines_used = np.zeros( (norders) ) - - velocities = vel_min + np.arange( N ) * vel_step - - Xcor_full[:,0] = velocities - - weight=0.0 - mask_middle = 0.5*(mask_l + mask_h) - W = np.zeros( norders ) - - vwt = 300 - for j in range(start_order,norders): - t1 = time.time() - LL = np.where( spectra[spec_order,j,:] != 0 ) - if len(LL[0]) > 0: - x1 = np.min( LL ) - x2 = np.max( LL ) - w1 = np.argmin( np.absolute( spectra[0,j,:] - spectra[0,j,x1] ) ) - w2 = np.argmin( np.absolute( spectra[0,j,:] - spectra[0,j,x2] ) ) - l1_0 = spectra[0,j,w1] / lbary_ltopo - l2_0 = spectra[0,j,w2] / lbary_ltopo - ww1 = np.argmin( np.abs( spectra[0,j,:] - l1_0*(1+(31+max_vel_rough)/c) ) ) - ww2 = np.argmin( np.abs( spectra[0,j,:] - l2_0*(1-(31+max_vel_rough)/c) ) ) - # should not happen, but hey, just in case... - if (ww1 < w1): - ww1 = w1 - if (ww2 > w2): - ww2 = w2 - l1 = spectra[0,j,ww1] - l2 = spectra[0,j,ww2] - II = np.where( (mask_l > l1) & (mask_h < l2) ) - #if len(II[0])>0: - #print j,II[0][0],II[0][-1] - nlu = len(II[0]) - nlines_used[j] = nlu - snw1 = int(0.25*spectra.shape[2]) - snw2 = int(0.75*spectra.shape[2]) - if (nlu > 0): - # calculate median S/N - #median_sn = np.median( spectra[5,j,w1:w2] * np.sqrt( spectra[6,j,w1:w2] ) ) - median_sn = np.median( spectra[sn_order,j,snw1:snw2] ) - sn[j] = median_sn - S = spectra[spec_order,j,w1:w2] - #iv = spectra[iv_order,j,w1:w2] - signal2noise = spectra[sn_order,j,w1:w2] - snwa = np.zeros(N) - for k in range(N): - #print k - Xcor_full[k,j+1], snw = CCF.ccfcos(mask_l[II], mask_h[II], spectra[0,j,w1:w2], S,\ - mask_w[II], signal2noise, vel_min + k*vel_step) - snwa[k] = snw - - if np.isnan(Xcor_full[k,j+1]): - Xcor_full[k,j+1] = Xcor_full[k-1,j+1] - snwa[k] = snwa[k-1] - #if k ==182 and j==35: - # #print mask_l[II], mask_h[II], spectra[0,j,w1:w2], S,mask_w[II], signal2noise, vel_min + k*vel_step - # #for z in range(len(mask_l[II])): - # # III = np.where((spectra[0,j,w1:w2]>=mask_l[II][z])&(spectra[0,j,w1:w2]<=mask_h[II][z]))[0] - # # print spectra[0,j,w1:w2][III],S[III] - # #print Xcor_full[k,j+1] - # #print snw - # #print gfd - - xc_weight = np.median( snwa ) - Xcor_full[:,j+1] /= snwa #xc_weight - W[j] = xc_weight - - return velocities, Xcor_full, sn, nlines_used, W + """ + Calculates the cross-correlation function for a Coralie Spectra + """ + # speed of light, km/s + c = 2.99792458E5 + + # loop over orders + norders = spectra.shape[1] + + # determine minimum velocities + vel_min = vel - vel_width + vel_max = vel + vel_width + N = int(np.ceil( (2*vel_width) / vel_step )) + + Xcor_full = np.zeros( (N, norders+1) ) + sn = np.zeros( (norders) ) + nlines_used = np.zeros( (norders) ) + + velocities = vel_min + np.arange( N ) * vel_step + + Xcor_full[:,0] = velocities + + weight=0.0 + mask_middle = 0.5*(mask_l + mask_h) + W = np.zeros( norders ) + + vwt = 300 + for j in range(start_order,norders): + t1 = time.time() + LL = np.where( spectra[spec_order,j,:] != 0 ) + if len(LL[0]) > 0: + x1 = np.min( LL ) + x2 = np.max( LL ) + w1 = np.argmin( np.absolute( spectra[0,j,:] - spectra[0,j,x1] ) ) + w2 = np.argmin( np.absolute( spectra[0,j,:] - spectra[0,j,x2] ) ) + l1_0 = spectra[0,j,w1] / lbary_ltopo + l2_0 = spectra[0,j,w2] / lbary_ltopo + ww1 = np.argmin( np.abs( spectra[0,j,:] - l1_0*(1+(31+max_vel_rough)/c) ) ) + ww2 = np.argmin( np.abs( spectra[0,j,:] - l2_0*(1-(31+max_vel_rough)/c) ) ) + # should not happen, but hey, just in case... + if (ww1 < w1): + ww1 = w1 + if (ww2 > w2): + ww2 = w2 + l1 = spectra[0,j,ww1] + l2 = spectra[0,j,ww2] + II = np.where( (mask_l > l1) & (mask_h < l2) ) + #if len(II[0])>0: + #print j,II[0][0],II[0][-1] + nlu = len(II[0]) + nlines_used[j] = nlu + snw1 = int(0.25*spectra.shape[2]) + snw2 = int(0.75*spectra.shape[2]) + if (nlu > 0): + # calculate median S/N + #median_sn = np.median( spectra[5,j,w1:w2] * np.sqrt( spectra[6,j,w1:w2] ) ) + median_sn = np.median( spectra[sn_order,j,snw1:snw2] ) + sn[j] = median_sn + S = spectra[spec_order,j,w1:w2] + #iv = spectra[iv_order,j,w1:w2] + signal2noise = spectra[sn_order,j,w1:w2] + snwa = np.zeros(N) + for k in range(N): + #print k + Xcor_full[k,j+1], snw = CCF.ccfcos(mask_l[II], mask_h[II], spectra[0,j,w1:w2], S,\ + mask_w[II], signal2noise, vel_min + k*vel_step) + snwa[k] = snw + + if np.isnan(Xcor_full[k,j+1]): + Xcor_full[k,j+1] = Xcor_full[k-1,j+1] + snwa[k] = snwa[k-1] + #if k ==182 and j==35: + # #print mask_l[II], mask_h[II], spectra[0,j,w1:w2], S,mask_w[II], signal2noise, vel_min + k*vel_step + # #for z in range(len(mask_l[II])): + # # III = np.where((spectra[0,j,w1:w2]>=mask_l[II][z])&(spectra[0,j,w1:w2]<=mask_h[II][z]))[0] + # # print spectra[0,j,w1:w2][III],S[III] + # #print Xcor_full[k,j+1] + # #print snw + # #print gfd + + xc_weight = np.median( snwa ) + Xcor_full[:,j+1] /= snwa #xc_weight + W[j] = xc_weight + + return velocities, Xcor_full, sn, nlines_used, W def XC_Herm_Fit(X,Y,back_lag=5, usemin=True, horder=20, sigma_res = 2, horder_res_herm=10, sigma_res_herm = 2.5): """ @@ -1498,24 +1502,24 @@ def fitfunc(p,x,horder, norms, herms): her = 0.0 xnorm = (x-p[1])/p[2] for l in range(3,horder+1): -# norm = 1.0 / np.sqrt(scipy.misc.factorial(l) * (2 ** l) ) +# norm = 1.0 / np.sqrt(scipy.misc.factorial(l) * (2 ** l) ) # her += p[4 + (l-3)] * norm*scipy.polyval( scipy.special.hermite( l ), x ) her += p[4 + (l-3) ] * norms[l-3] * scipy.polyval( herms[l-3], xnorm ) return gau*(1.0 + her) + 1.0 + p[3] - + def errfunc(p, x, y, h, norms, herms): clutch = 0.0 mean = p[1] if (mean < np.min(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.min(x)) / 3) ) - if (mean > np.max(x)): + if (mean > np.max(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.max(x)) / 3) ) return np.ravel( (fitfunc(p,x,h,norms, herms) - y) ) + clutch if (horder >= 3): n = 4 + (horder-2) else: - n = 4 + n = 4 norms, herms = get_herms(horder) @@ -1535,18 +1539,18 @@ def errfunc(p, x, y, h, norms, herms): if (horder < horder_res_herm): horder_res_herm = horder - + # now run h=horder_res_herm and Gaussian fit to +- sigma_res* sigma of mean mean = p1[1] sigma = p1[2] - + L1 = np.where( np.abs(X - mean) <= sigma_res_herm* sigma) horder=horder_res_herm if (horder >= 3): n = 4 + (horder-2) else: n = 4 - + if (len(L1[0]) > 0): norms, herms = get_herms(horder) p0 = p0[0:n] @@ -1559,7 +1563,7 @@ def errfunc(p, x, y, h, norms, herms): L2 = np.where( np.abs(X - mean) <= sigma_res * sigma) # the following is just a Gaussian fit horder = 2 - if (len(L2[0]) > 0): + if (len(L2[0]) > 0): norms, herms = get_herms(horder) p0 = p0[0:4] p1_gau, success_gau = scipy.optimize.leastsq(errfunc,p0, args=(X[L2],Y[L2],horder,norms,herms)) @@ -1567,7 +1571,7 @@ def errfunc(p, x, y, h, norms, herms): else: p1_gau = np.zeros(n) predicted_gau = np.zeros( len(X[L2]) ) - + return p1, predicted, p1_s, predicted_s, p1_gau, predicted_gau, L1, L2 #return p1, predicted, p1_gau, predicted_gau, L2 @@ -1587,13 +1591,13 @@ def fitfunc(p,x,horder, norms, herms): for l in range(3,horder+1): her += p[4 + (l-3) - 1] * norms[l-3] * scipy.polyval( herms[l-3], xnorm ) return gau*(1.0 + her) + 1.0 # + p[3] - + def errfunc(p, x, y, h, norms, herms): clutch = 0.0 mean = p[1] if (mean < np.min(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.min(x)) / 3) ) - if (mean > np.max(x)): + if (mean > np.max(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.max(x)) / 3) ) return np.ravel( (fitfunc(p,x,h,norms, herms) - y) ) + clutch @@ -1604,13 +1608,13 @@ def fitfunc2(p,x,horder, norms, herms, moonv,moons): for l in range(3,horder+1): her += p[4 + (l-3) - 1] * norms[l-3] * scipy.polyval( herms[l-3], xnorm ) return gau*(1.0 + her) + 1.0 # + p[3] - + def errfunc2(p, x, y, h, norms, herms, moonv,moons): clutch = 0.0 mean = p[1] if (mean < np.min(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.min(x)) / 3) ) - if (mean > np.max(x)): + if (mean > np.max(x)): clutch = 1e10*(1.0 - exp(-np.abs(mean-np.max(x)) / 3) ) return np.ravel( (fitfunc2(p,x,h,norms, herms, moonv,moons) - y) ) + clutch @@ -1630,7 +1634,7 @@ def errfunc2(p, x, y, h, norms, herms, moonv,moons): p0[0] = np.min(Y) - (1.0 + p0[3]) else: p0[0] = np.max(Y) - (1.0 + p0[3]) - + p1, success = scipy.optimize.leastsq(errfunc,p0, args=(X,Y,horder,norms, herms)) predicted = fitfunc(p1,X,horder,norms,herms) mean = p1[1] @@ -1649,7 +1653,7 @@ def errfunc2(p, x, y, h, norms, herms, moonv,moons): L2 = np.where( np.abs(X - mean) <= sigma_res * sigma0) - if (len(L2[0]) > 0): + if (len(L2[0]) > 0): norms, herms = get_herms(horder) p1_gau0, success_gau = scipy.optimize.leastsq(errfunc,p0, args=(X[L2],Y[L2],horder,norms,herms)) else: @@ -1661,23 +1665,23 @@ def errfunc2(p, x, y, h, norms, herms, moonv,moons): L2 = np.where( np.abs(X - mean) <= sigma_res * sigma) if moon: - p1_gau0 = np.append(p1_gau0, f0) - if (len(L2[0]) > 0): - norms, herms = get_herms(horder) - p1_gau, success_gau = scipy.optimize.leastsq(errfunc2,p1_gau0, args=(X[L2],Y[L2],horder,norms,herms,moonv,moons)) - predicted_gau = fitfunc2(p1_gau,X[L2],horder,norms,herms,moonv,moons) - else: - p1_gau = np.zeros(n) - predicted_gau = np.zeros( len(X[L2]) ) + p1_gau0 = np.append(p1_gau0, f0) + if (len(L2[0]) > 0): + norms, herms = get_herms(horder) + p1_gau, success_gau = scipy.optimize.leastsq(errfunc2,p1_gau0, args=(X[L2],Y[L2],horder,norms,herms,moonv,moons)) + predicted_gau = fitfunc2(p1_gau,X[L2],horder,norms,herms,moonv,moons) + else: + p1_gau = np.zeros(n) + predicted_gau = np.zeros( len(X[L2]) ) else: - if (len(L2[0]) > 0): - norms, herms = get_herms(horder) - p1_gau, success_gau = scipy.optimize.leastsq(errfunc,p1_gau0, args=(X[L2],Y[L2],horder,norms,herms)) - predicted_gau = fitfunc(p1_gau,X[L2],horder,norms,herms) - else: - p1_gau = np.zeros(n) - predicted_gau = np.zeros( len(X[L2]) ) - + if (len(L2[0]) > 0): + norms, herms = get_herms(horder) + p1_gau, success_gau = scipy.optimize.leastsq(errfunc,p1_gau0, args=(X[L2],Y[L2],horder,norms,herms)) + predicted_gau = fitfunc(p1_gau,X[L2],horder,norms,herms) + else: + p1_gau = np.zeros(n) + predicted_gau = np.zeros( len(X[L2]) ) + return p1, predicted, p1_gau, predicted_gau, L2 @@ -1695,13 +1699,13 @@ def Average_CCF(xc_full, sn, start_order=0,sn_min=0.15, Simple=False, W=None, bo combine_indices = range(start_order, norders) #print combine_indices if len(boot_ind)>0: - combine_indices = boot_ind + combine_indices = boot_ind for order in combine_indices: - #I = np.where(np.isnan(xc_full[:,order+1])) - #print I + #I = np.where(np.isnan(xc_full[:,order+1])) + #print I sum_xc = np.sum( xc_full[:,order+1] ) - #print order, sum_xc, xc_full[:,order+1] + #print order, sum_xc, xc_full[:,order+1] if (sum_xc > 0): if (Simple): norm = np.median( xc_full[:,order+1] ) @@ -1720,66 +1724,66 @@ def Average_CCF(xc_full, sn, start_order=0,sn_min=0.15, Simple=False, W=None, bo if (sn[order] > sn_min): ws += weight_sn xc_av += weight_sn * CCF_norm - + IN = np.where(xc_av!=0)[0] if len(IN) == 0 or ws == 0: - xc_av = np.ones(len(xc_av)) + xc_av = np.ones(len(xc_av)) else: xc_av /= ws return xc_av def IntGaussian(x,mu,sigma): - """ + """ - Returns Gaussian integrated over a pixel + Returns Gaussian integrated over a pixel """ s2 = sqrt(2) arg1 = (x+0.5-mu)/(s2*sigma) arg2 = (x-0.5-mu)/(s2*sigma) - ret = 0.5*(special.erf(arg1) - special.erf(arg2)) + ret = 0.5*(special.erf(arg1) - special.erf(arg2)) return ret def CorGaussian(x,mu,sigma): - """ Returns Gaussian """ - norm = 1.0 / (np.sqrt(2 * np.pi) * sigma) - clutch = 0 - if (sigma < 0): - clutch = 1e32 - return norm * np.exp(-0.5*((x-mu)**2) / sigma**2) + clutch + """ Returns Gaussian """ + norm = 1.0 / (np.sqrt(2 * np.pi) * sigma) + clutch = 0 + if (sigma < 0): + clutch = 1e32 + return norm * np.exp(-0.5*((x-mu)**2) / sigma**2) + clutch def CorGaussian2(x,mu,sigma,mu2,sigma2,f): - """ Returns Gaussian """ - norm = 1.0 / (np.sqrt(2 * np.pi) * sigma) - norm2 = 1.0 / (np.sqrt(2 * np.pi) * sigma2) - clutch = 0 - if (sigma < 0): - clutch = 1e32 - return norm * np.exp(-0.5*((x-mu)**2) / sigma**2) + clutch + f * np.exp(-0.5*((x-mu2)**2) / sigma2**2) + """ Returns Gaussian """ + norm = 1.0 / (np.sqrt(2 * np.pi) * sigma) + norm2 = 1.0 / (np.sqrt(2 * np.pi) * sigma2) + clutch = 0 + if (sigma < 0): + clutch = 1e32 + return norm * np.exp(-0.5*((x-mu)**2) / sigma**2) + clutch + f * np.exp(-0.5*((x-mu2)**2) / sigma2**2) def get_rough_offset(sc,files,window=100): i = 0 xct = [] while i < len(files): - spec = sc[i] - f = open(files[i]).readlines() - pixel_centers_0 = [] - for line in f: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])) - - ml = array(pixel_centers_0) - 2 - mh = array(pixel_centers_0) + 2 - xc,offs = XCorPix( spec, ml, mh,del_width=window) - if len(xct) == 0: - xct = xc - else: - xct += xc - i+=1 - + spec = sc[i] + f = open(files[i]).readlines() + pixel_centers_0 = [] + for line in f: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])) + + ml = array(pixel_centers_0) - 2 + mh = array(pixel_centers_0) + 2 + xc,offs = XCorPix( spec, ml, mh,del_width=window) + if len(xct) == 0: + xct = xc + else: + xct += xc + i+=1 + ind_max = np.argmax( xct ) delta = offs[ind_max] #plot(offs,xct) @@ -1787,106 +1791,106 @@ def get_rough_offset(sc,files,window=100): return delta def fit_these_lines(waves_ob,filename,spec,order,wei, rough_shift = 0.0, del_width=5.0, binning=1,\ - line_width=4, fact=1,do_xc=True,sigmai=2.2): - - f = open(filename).readlines() - pixel_centers = array([]) - wavelengths = array([]) - sigmas = array([]) - centroids = array([]) - intensities = array([]) - - if do_xc: - pixel_centers_0 = [] - for line in f: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])*fact/float(binning) + rough_shift) - ml = array(pixel_centers_0) - 2 - mh = array(pixel_centers_0) + 2 - xc,offs = XCorPix( spec, ml, mh, del_width=del_width) - ind_max = np.argmax( xc ) - delta = offs[ind_max] - else: - delta=0. - - N_l = 0 - bad_indices = [] - bad_indices_ct = 0 - #plot(spec) - for line in f: - #print line - w = line.split() - # extract info, and fit line(s) - nlines = int(w[0]) - pix = [] - wav = [] - for j in range(nlines): - if float(w[2*j+1])*fact/float(binning) + rough_shift+delta > 20 and float(w[2*j+1])*fact/float(binning) + rough_shift+delta < len(spec)-20: - pix.append(float(w[2*j+1])*fact/float(binning) + delta + rough_shift) - wav.append(float(w[2*j+2])) - if len(pix) > 0: - N_l += len(pix) - pix = np.array(pix) - #pix2=np.around(pix).astype('int') - #plot(pix2,spec[pix2],'ro') - wav = np.array(wav) - xmin = int(round(min(pix))) - xmax = int(round(max(pix))) - X = array(range(xmin-line_width,xmax+line_width+1)) - Y = spec[xmin-line_width:xmax+line_width+1] - if (nlines == 1): - num = np.sum(X*Y) - den = np.sum(Y) - if (den > 0): - Cent = num/den - else: - Cent = -1 - weight = wei[xmin-line_width:xmax+line_width+1] - kk = np.where( weight == 0) - # Input Spectrum is background subtracted ---> B=0 - B = np.zeros(len(X)) - mu = pix - sigma = np.zeros(nlines) + sigmai * fact / float(binning) - #plot(X,Y) - #show() - #print X, Y, B, mu, sigma, weight - p1, suc = LineFit_SingleSigma( X, Y, B, mu, sigma, weight) - #print p1 - #if (suc<1) or (suc > 4): - # print "Problem", order, X, delta - # collect fit information - #plot(X,Y) - reto = p1[0]*np.exp((X-p1[1])**2/(-0.5*p1[2]**2)) - #print reto - #plot(X,reto,'r') - - wavelenghts = np.append(wavelengths,wav) - for j in range(nlines): - pixel_centers = np.append(pixel_centers,p1[3*j + 1]) - sigmas = np.append(sigmas,p1[3*j + 2]) - wavelengths = np.append(wavelengths,wav[j]) - intensities = np.append(intensities,p1[3*j]) - if (nlines == 1): - centroids = np.append(centroids, Cent) - else: - centroids = np.append(centroids, -1) - wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co = [],[],[],[],[] - for i in range(len(wavelengths)): - if np.around(wavelengths[i],4) in np.around(waves_ob,4): - wavelengths_co.append(wavelengths[i]) - pixel_centers_co.append(pixel_centers[i]) - intensities_co.append(intensities[i]) - sigmas_co.append(sigmas[i]) - centroids_co.append(centroids[i]) - wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co = \ - np.array(wavelengths_co),np.array(pixel_centers_co),np.array(intensities_co), \ - np.array(sigmas_co),np.array(centroids_co) - #plot(np.around(pixel_centers_co).astype('int'),spec[np.around(pixel_centers_co).astype('int')],'ro') - #show() - - return wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co + line_width=4, fact=1,do_xc=True,sigmai=2.2): + + f = open(filename).readlines() + pixel_centers = array([]) + wavelengths = array([]) + sigmas = array([]) + centroids = array([]) + intensities = array([]) + + if do_xc: + pixel_centers_0 = [] + for line in f: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])*fact/float(binning) + rough_shift) + ml = array(pixel_centers_0) - 2 + mh = array(pixel_centers_0) + 2 + xc,offs = XCorPix( spec, ml, mh, del_width=del_width) + ind_max = np.argmax( xc ) + delta = offs[ind_max] + else: + delta=0. + + N_l = 0 + bad_indices = [] + bad_indices_ct = 0 + #plot(spec) + for line in f: + #print line + w = line.split() + # extract info, and fit line(s) + nlines = int(w[0]) + pix = [] + wav = [] + for j in range(nlines): + if float(w[2*j+1])*fact/float(binning) + rough_shift+delta > 20 and float(w[2*j+1])*fact/float(binning) + rough_shift+delta < len(spec)-20: + pix.append(float(w[2*j+1])*fact/float(binning) + delta + rough_shift) + wav.append(float(w[2*j+2])) + if len(pix) > 0: + N_l += len(pix) + pix = np.array(pix) + #pix2=np.around(pix).astype('int') + #plot(pix2,spec[pix2],'ro') + wav = np.array(wav) + xmin = int(round(min(pix))) + xmax = int(round(max(pix))) + X = array(range(xmin-line_width,xmax+line_width+1)) + Y = spec[xmin-line_width:xmax+line_width+1] + if (nlines == 1): + num = np.sum(X*Y) + den = np.sum(Y) + if (den > 0): + Cent = num/den + else: + Cent = -1 + weight = wei[xmin-line_width:xmax+line_width+1] + kk = np.where( weight == 0) + # Input Spectrum is background subtracted ---> B=0 + B = np.zeros(len(X)) + mu = pix + sigma = np.zeros(nlines) + sigmai * fact / float(binning) + #plot(X,Y) + #show() + #print X, Y, B, mu, sigma, weight + p1, suc = LineFit_SingleSigma( X, Y, B, mu, sigma, weight) + #print p1 + #if (suc<1) or (suc > 4): + # print "Problem", order, X, delta + # collect fit information + #plot(X,Y) + reto = p1[0]*np.exp((X-p1[1])**2/(-0.5*p1[2]**2)) + #print reto + #plot(X,reto,'r') + + wavelenghts = np.append(wavelengths,wav) + for j in range(nlines): + pixel_centers = np.append(pixel_centers,p1[3*j + 1]) + sigmas = np.append(sigmas,p1[3*j + 2]) + wavelengths = np.append(wavelengths,wav[j]) + intensities = np.append(intensities,p1[3*j]) + if (nlines == 1): + centroids = np.append(centroids, Cent) + else: + centroids = np.append(centroids, -1) + wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co = [],[],[],[],[] + for i in range(len(wavelengths)): + if np.around(wavelengths[i],4) in np.around(waves_ob,4): + wavelengths_co.append(wavelengths[i]) + pixel_centers_co.append(pixel_centers[i]) + intensities_co.append(intensities[i]) + sigmas_co.append(sigmas[i]) + centroids_co.append(centroids[i]) + wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co = \ + np.array(wavelengths_co),np.array(pixel_centers_co),np.array(intensities_co), \ + np.array(sigmas_co),np.array(centroids_co) + #plot(np.around(pixel_centers_co).astype('int'),spec[np.around(pixel_centers_co).astype('int')],'ro') + #show() + + return wavelengths_co,pixel_centers_co,intensities_co,sigmas_co,centroids_co @@ -1894,167 +1898,167 @@ def Initial_Wav_Calibration(filename,spec,order,wei, porder=3, rmsmax=75, minlin Dump_Argon=False, Dump_AllLines=False, Cheby=False, rough_shift = 0.0,\ del_width=5.0, binning=1,line_width=4, fact=1,do_xc=True,sigmai=2.2,pixelization=False): - f = open(filename).readlines() - pixel_centers = array([]) - wavelengths = array([]) - sigmas = array([]) - centroids = array([]) - intensities = array([]) - - if do_xc: - pixel_centers_0 = [] - for line in f: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])*fact/float(binning) + rough_shift) - - ml = array(pixel_centers_0) - 2 - mh = array(pixel_centers_0) + 2 - xc,offs = XCorPix( spec, ml, mh, del_width=del_width) - ind_max = np.argmax( xc ) - delta = offs[ind_max] - else: - delta=0. - - #print "Computed offset for order ", order, " is ", delta - N_l = 0 - bad_indices = [] - bad_indices_ct = 0 - #print order - #plot(spec) - for line in f: - if line[0]!='#': - w = line.split() - # extract info, and fit line(s) - nlines = int(w[0]) - pix = [] - wav = [] - for j in range(nlines): - if float(w[2*j+1])*fact/float(binning) + rough_shift+delta > 20 and \ - float(w[2*j+1])*fact/float(binning) + rough_shift+delta < len(spec)-20: - pix.append(float(w[2*j+1])*fact/float(binning) + delta + rough_shift) - wav.append(float(w[2*j+2])) - - if len(pix) > 0: - pix = np.array(pix) - wav = np.array(wav) - xmin = int(round(min(pix))) - xmax = int(round(max(pix))) - X = array(range(xmin-line_width,xmax+line_width+1)) - Y = spec[xmin-line_width:xmax+line_width+1] - if len(np.where(Y!=0)[0])>0: - N_l += len(pix) - if (nlines == 1): - num = np.sum(X*Y) - den = np.sum(Y) - if (den > 0): - Cent = num/den - else: - Cent = -1 - - weight = wei[xmin-line_width:xmax+line_width+1] - kk = np.where( weight == 0) - # Input Spectrum is background subtracted ---> B=0 - B = np.zeros(len(X)) - mu = pix - sigma = np.zeros(nlines) + sigmai * fact / float(binning) - - p1, suc = LineFit_SingleSigma( X, Y, B, mu, sigma, weight,pixelization=pixelization) - #print p1 - #if (suc<1) or (suc > 4): - # print "Problem", order, X, delta - # collect fit information - #reto = fitfunc_temp(p1, X, len(pix),pixelization= pixelization) - #plot(X,reto,'r') - wavelenghts = np.append(wavelengths,wav) - for j in range(len(pix)): - pixel_centers = np.append(pixel_centers,p1[3*j + 1]) - sigmas = np.append(sigmas,p1[3*j + 2]) - wavelengths = np.append(wavelengths,wav[j]) - intensities = np.append(intensities,p1[3*j]) - if (nlines == 1): - centroids = np.append(centroids, Cent) - else: - centroids = np.append(centroids, -1) - - #print len(pixel_centers) - pixel_centers2 = np.around(pixel_centers).astype('int') - I = np.where((pixel_centers2>=0) & (pixel_centers20) & (pixel_centers<2048))[0] - #plot(np.around(pixel_centers[I]).astype('int'),spec[np.around(pixel_centers[I]).astype('int')],'ro') - #show() - I1 = np.where(pixel_centers<50)[0] - I2 = np.where(pixel_centers>len(spec)-50)[0] - II = np.hstack((I1,I2)) - bad_indices = np.hstack((np.array(bad_indices),II)) - bad_indices = list(np.unique(bad_indices)) - # now, do the polynomial fit, rejecting some lines until RMS is below rmsmax - I = range( N_l ) - - for bi in bad_indices: - I.remove( bi ) - N_l -= 1 - - if (Cheby): - coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) - coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) - else: - coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) - coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) - - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - - if (FixEnds): - minI = np.min( I ) + 1 - maxI = np.max( I ) - 1 - else: - minI = np.min( I ) - maxI = np.max( I ) - #if order==26: - # plot(pixel_centers[I],residuals,'ro') - # plot([0,4096],[0,0]) - #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) - #show() - #print dfgh - count = 0 - while ((N_l > minlines) and (rmsms > rmsmax)): - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - index_worst = np.argmax( np.absolute(residuals) ) - I.pop( index_worst) - N_l -= 1 - if (Cheby): - coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) - coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) - else: - coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) - coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) - count +=1 - - rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) - - pci = np.around(pixel_centers).astype('int') - #plot(spec) - #plot(pci,spec[pci],'ro') - #plot(pci[I],spec[pci[I]],'bo') - #show() - #plot(wavelengths[I],residuals,'ro') - #show() - #print "RMS is ", rmsms, "using ", N_l, " lines at indices ", I - #plot(pixel_centers[I],wavelengths[I],'ro') - #if order == 26: - # plot(pixel_centers[I],residuals-0.1,'bo') - # plot([0,4096],[-0.1,-0.1]) - # #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) - # show() - #print order, len(pixel_centers), len(I) - return coeffs_pix2wav, coeffs_pix2sigma, pixel_centers[I], wavelengths[I], \ - rmsms, residuals, centroids[I], sigmas[I], intensities[I] + f = open(filename).readlines() + pixel_centers = array([]) + wavelengths = array([]) + sigmas = array([]) + centroids = array([]) + intensities = array([]) + + if do_xc: + pixel_centers_0 = [] + for line in f: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])*fact/float(binning) + rough_shift) + + ml = array(pixel_centers_0) - 2 + mh = array(pixel_centers_0) + 2 + xc,offs = XCorPix( spec, ml, mh, del_width=del_width) + ind_max = np.argmax( xc ) + delta = offs[ind_max] + else: + delta=0. + + #print "Computed offset for order ", order, " is ", delta + N_l = 0 + bad_indices = [] + bad_indices_ct = 0 + #print order + #plot(spec) + for line in f: + if line[0]!='#': + w = line.split() + # extract info, and fit line(s) + nlines = int(w[0]) + pix = [] + wav = [] + for j in range(nlines): + if float(w[2*j+1])*fact/float(binning) + rough_shift+delta > 20 and \ + float(w[2*j+1])*fact/float(binning) + rough_shift+delta < len(spec)-20: + pix.append(float(w[2*j+1])*fact/float(binning) + delta + rough_shift) + wav.append(float(w[2*j+2])) + + if len(pix) > 0: + pix = np.array(pix) + wav = np.array(wav) + xmin = int(round(min(pix))) + xmax = int(round(max(pix))) + X = array(range(xmin-line_width,xmax+line_width+1)) + Y = spec[xmin-line_width:xmax+line_width+1] + if len(np.where(Y!=0)[0])>0: + N_l += len(pix) + if (nlines == 1): + num = np.sum(X*Y) + den = np.sum(Y) + if (den > 0): + Cent = num/den + else: + Cent = -1 + + weight = wei[xmin-line_width:xmax+line_width+1] + kk = np.where( weight == 0) + # Input Spectrum is background subtracted ---> B=0 + B = np.zeros(len(X)) + mu = pix + sigma = np.zeros(nlines) + sigmai * fact / float(binning) + + p1, suc = LineFit_SingleSigma( X, Y, B, mu, sigma, weight,pixelization=pixelization) + #print p1 + #if (suc<1) or (suc > 4): + # print "Problem", order, X, delta + # collect fit information + #reto = fitfunc_temp(p1, X, len(pix),pixelization= pixelization) + #plot(X,reto,'r') + wavelenghts = np.append(wavelengths,wav) + for j in range(len(pix)): + pixel_centers = np.append(pixel_centers,p1[3*j + 1]) + sigmas = np.append(sigmas,p1[3*j + 2]) + wavelengths = np.append(wavelengths,wav[j]) + intensities = np.append(intensities,p1[3*j]) + if (nlines == 1): + centroids = np.append(centroids, Cent) + else: + centroids = np.append(centroids, -1) + + #print len(pixel_centers) + pixel_centers2 = np.around(pixel_centers).astype('int') + I = np.where((pixel_centers2>=0) & (pixel_centers20) & (pixel_centers<2048))[0] + #plot(np.around(pixel_centers[I]).astype('int'),spec[np.around(pixel_centers[I]).astype('int')],'ro') + #show() + I1 = np.where(pixel_centers<50)[0] + I2 = np.where(pixel_centers>len(spec)-50)[0] + II = np.hstack((I1,I2)) + bad_indices = np.hstack((np.array(bad_indices),II)) + bad_indices = list(np.unique(bad_indices)) + # now, do the polynomial fit, rejecting some lines until RMS is below rmsmax + I = range( N_l ) + + for bi in bad_indices: + I.remove( bi ) + N_l -= 1 + + if (Cheby): + coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) + coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) + else: + coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) + coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) + + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + + if (FixEnds): + minI = np.min( I ) + 1 + maxI = np.max( I ) - 1 + else: + minI = np.min( I ) + maxI = np.max( I ) + #if order==26: + # plot(pixel_centers[I],residuals,'ro') + # plot([0,4096],[0,0]) + #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) + #show() + #print dfgh + count = 0 + while ((N_l > minlines) and (rmsms > rmsmax)): + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + index_worst = np.argmax( np.absolute(residuals) ) + I.pop( index_worst) + N_l -= 1 + if (Cheby): + coeffs_pix2wav = Cheby_Fit(pixel_centers[I], wavelengths[I], porder,len(spec)) + coeffs_pix2sigma = Cheby_Fit(pixel_centers[I], sigmas[I], porder,len(spec)) + else: + coeffs_pix2wav = scipy.polyfit(pixel_centers[I], wavelengths[I], porder) + coeffs_pix2sigma = scipy.polyfit(pixel_centers[I], sigmas[I], porder) + count +=1 + + rmsms, residuals = rms_ms(coeffs_pix2wav, pixel_centers[I], wavelengths[I], len(spec), Cheby=Cheby) + + pci = np.around(pixel_centers).astype('int') + #plot(spec) + #plot(pci,spec[pci],'ro') + #plot(pci[I],spec[pci[I]],'bo') + #show() + #plot(wavelengths[I],residuals,'ro') + #show() + #print "RMS is ", rmsms, "using ", N_l, " lines at indices ", I + #plot(pixel_centers[I],wavelengths[I],'ro') + #if order == 26: + # plot(pixel_centers[I],residuals-0.1,'bo') + # plot([0,4096],[-0.1,-0.1]) + # #plot(np.arange(4096),Cheby_eval(coeffs_pix2wav,np.arange(4096),len(spec))) + # show() + #print order, len(pixel_centers), len(I) + return coeffs_pix2wav, coeffs_pix2sigma, pixel_centers[I], wavelengths[I], \ + rmsms, residuals, centroids[I], sigmas[I], intensities[I] def XCorPix(spectra, mask_l, mask_h, del0=0, del_width=5, del_step=0.1): """ @@ -2070,7 +2074,7 @@ def XCorPix(spectra, mask_l, mask_h, del0=0, del_width=5, del_step=0.1): N = int(np.ceil( (2*del_width) / del_step ).astype('int')) Xcor = np.zeros( N ) - + deltas = del_min + np.arange( N ) * del_step LL = np.where( spectra != 0 ) @@ -2094,7 +2098,7 @@ def rms_ms(coeffs_pix2wav, pixel_centers, wavelengths, npix, Cheby=False): central_wav = 0.5 * (scipy.polyval(coeffs_pix2wav,50.) + scipy.polyval(coeffs_pix2wav,npix-50)) rms_ms = np.sqrt( np.var( residuals ) ) * 299792458.0 / central_wav - + return rms_ms, residuals def Cheby_Fit(x,y,order,npix): @@ -2112,7 +2116,7 @@ def fitfunc(p,chebs,order): ret_val += p[order-i]*chebs[i] return ret_val errfunc = lambda p,chebs,y,order: np.ravel( (fitfunc(p,chebs,order)-y) ) - + def get_chebs(x,order): chebs = [] for i in range(0,order+1): @@ -2135,33 +2139,33 @@ def Cheby_eval(p,x,npix): ret_val = 0.0 for i in range(order + 1): ret_val += p[order - i]*scipy.special.chebyt(i)(x_norm) - + return ret_val - + def fitfunc_temp(p, x, n, pixelization=True): - - if pixelization: - lxo = len(x) - xo = x.copy() - x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) - ret = np.zeros(len(x)) - for i in range(n): - ret += ( p[i*2+1] * IntGaussian(x,p[i*2+2],p[0]) ) + if pixelization: + lxo = len(x) + xo = x.copy() + x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) + + ret = np.zeros(len(x)) + for i in range(n): + ret += ( p[i*2+1] * IntGaussian(x,p[i*2+2],p[0]) ) - if pixelization: - ret = ret.reshape((lxo,100)) - ret = np.mean(ret,axis=1) + if pixelization: + ret = ret.reshape((lxo,100)) + ret = np.mean(ret,axis=1) - return ret + return ret def LineFit_SingleSigma(X, Y, B, mu, sigma, weight,pixelization=False): """ This function fits a series of Gaussians simultaneously, given a set of input pixels, sigmas, and intensities - + Sigma is the same for all lines - + Returns (mu_i, sigm_i, int_i), i=1,.,n where n is the number of components """ @@ -2170,18 +2174,18 @@ def LineFit_SingleSigma(X, Y, B, mu, sigma, weight,pixelization=False): def fitfunc(p, x, n): - if pixelization: - lxo = len(x) - xo = x.copy() - x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) + if pixelization: + lxo = len(x) + xo = x.copy() + x = np.arange(x[0]-0.5,x[-1]+0.5,0.01) ret = np.zeros(len(x)) for i in range(n): ret += ( p[i*2+1] * IntGaussian(x,p[i*2+2],p[0]) ) - if pixelization: - ret = ret.reshape((lxo,100)) - ret = np.mean(ret,axis=1) + if pixelization: + ret = ret.reshape((lxo,100)) + ret = np.mean(ret,axis=1) return ret @@ -2189,7 +2193,7 @@ def fitfunc(p, x, n): # Build error array #err = sqrt((B+Y)/gain + readnoise**2) - + # Build input parameters list # length of inout parameter list: 3 * n p0 = np.zeros(2*n+1) @@ -2226,7 +2230,7 @@ def fitfunc(p, x, m): ret = (1.0/m) * Joint_Polynomial(p,x,m) return ret errfunc = lambda p,x,m,y: np.ravel( (fitfunc(p,x,m)-y) ) - + def fitfunc_cheb(p,chebs,m): ret = (1.0/m) * Joint_Polynomial_Cheby(p,chebs,nx=nx,nm=nm) return ret @@ -2235,7 +2239,7 @@ def fitfunc_cheb(p,chebs,m): #print order0, ntotal, npix, nx, nm, minlines, maxrms, Cheby, Inv #print gfd - + if (Cheby): chebs = Calculate_chebs(pix_centers, orders+order0, Inverse=Inv,order0=order0,ntotal=ntotal,npix=npix,nx=nx,nm=nm) p1, success = scipy.optimize.leastsq(errfunc_cheb, p0, args=(chebs, wavelengths, orders + order0, Wgt)) @@ -2245,28 +2249,28 @@ def fitfunc_cheb(p,chebs,m): residuals = errfunc(p1, pix_centers, orders + order0, wavelengths) #for oo in np.unique(orders): - # III = np.where(orders==oo)[0] - # plot(wavelengths[III],residuals[III],'.') + # III = np.where(orders==oo)[0] + # plot(wavelengths[III],residuals[III],'.') #show() residuals_ms = 299792458.0 * residuals / wavelengths - rms_ms = np.sqrt( np.var( residuals_ms ) ) + rms_ms = np.sqrt( np.var( residuals_ms ) ) N_l = len( pix_centers ) I = range( N_l ) cond = 1 L = np.where( np.absolute(residuals_ms) > 4.0*rms_ms ) - if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): + if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): cond=0 - - print "\t\t\tStart Global culling with ", N_l, " number of lines" + + print("\t\t\tStart Global culling with ", N_l, " number of lines") bad_wavs, bad_ords = [],[] - while (cond): + while (cond): index_worst = np.argmax( np.absolute(residuals) ) - bad_wavs.append(wavelengths[I[index_worst]]) - bad_ords.append(orders[I[index_worst]]) - #print orders[I[index_worst]],wavelengths[I[index_worst]],rms_ms + bad_wavs.append(wavelengths[I[index_worst]]) + bad_ords.append(orders[I[index_worst]]) + #print orders[I[index_worst]],wavelengths[I[index_worst]],rms_ms I.pop( index_worst ) N_l -= 1 if (Cheby): @@ -2278,33 +2282,33 @@ def fitfunc_cheb(p,chebs,m): residuals = errfunc(p1, pix_centers[I], orders[I] + order0, wavelengths[I]) residuals_ms = 299792458.0 * residuals / wavelengths[I] - rms_ms = np.sqrt( np.var( residuals_ms ) ) + rms_ms = np.sqrt( np.var( residuals_ms ) ) L = np.where( np.absolute(residuals_ms) > 4.*rms_ms ) - #print rms_ms, maxrms, len(L[0]) - if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): + #print rms_ms, maxrms, len(L[0]) + if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): cond=0 #print "Eliminated line ", index_worst, " at order ", orders[index_worst] #print "RMS is ", rms_ms, " after elimination", len(L[0]), rms_ms, maxrms, N_l #for oo in np.unique(orders[I]): - # III = np.where(orders[I]==oo)[0] - # plot(wavelengths[I][III],residuals[III],'.') - # plot(np.median(wavelengths[I][III]),np.median(residuals[III]),'ko') + # III = np.where(orders[I]==oo)[0] + # plot(wavelengths[I][III],residuals[III],'.') + # plot(np.median(wavelengths[I][III]),np.median(residuals[III]),'ko') #show() """ bad_wavs,bad_ords = np.array(bad_wavs), np.array(bad_ords) for i in np.unique(bad_ords): - J = np.where(bad_ords==i)[0] - tmpwvs,tmpords = bad_wavs[J],bad_ords[J] - ist = np.argsort(tmpwvs) - for j in ist: - print tmpords[j],tmpwvs[j] + J = np.where(bad_ords==i)[0] + tmpwvs,tmpords = bad_wavs[J],bad_ords[J] + ist = np.argsort(tmpwvs) + for j in ist: + print tmpords[j],tmpwvs[j] """ - print "\t\t\tFinal RMS is ", rms_ms - print "\t\t\tNumber of lines is ", N_l - print "\t\t\t--> Achievable RV precision is ", rms_ms/np.sqrt(N_l) + print("\t\t\tFinal RMS is ", rms_ms) + print("\t\t\tNumber of lines is ", N_l) + print("\t\t\t--> Achievable RV precision is ", rms_ms/np.sqrt(N_l)) - return p1, pix_centers[I], orders[I], wavelengths[I], I, rms_ms, residuals + return p1, pix_centers[I], orders[I], wavelengths[I], I, rms_ms, residuals def Global_Wav_Solution_vel_shift(pix_centers, wavelengths, orders, Wgt, p_ref, minlines=1000, maxrms=150, order0=89, ntotal= 70, npix=2048, Cheby=False, Inv = False,nx=5,nm=6): """ @@ -2320,7 +2324,7 @@ def fitfunc(p, p_ref,x, m): ret = ((1+1e-6*p)/m) * Joint_Polynomial(p_ref,x,m) return ret errfunc = lambda p,p_ref,x,m,y: np.ravel( (fitfunc(p,p_ref,x,m)-y) ) - + def fitfunc_cheb(p,p_ref,chebs,m): ret = ((1+1e-6*p)/m) * Joint_Polynomial_Cheby(p_ref,chebs,nx=nx,nm=nm) return ret @@ -2339,23 +2343,23 @@ def fitfunc_cheb(p,p_ref,chebs,m): residuals = errfunc(p1, p_ref, pix_centers, orders + order0, wavelengths) residuals_ms = 299792458.0 * residuals / wavelengths - rms_ms = np.sqrt( np.var( residuals_ms ) ) + rms_ms = np.sqrt( np.var( residuals_ms ) ) N_l = len( pix_centers ) I = range( N_l ) cond = 1 L = np.where( np.absolute(residuals_ms) > 4.0*rms_ms ) - if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): + if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): cond=0 - + #for oo in np.unique(orders[I]): - # III = np.where(orders[I]==oo)[0] - # plot(wavelengths[I][III],residuals[III],'.') + # III = np.where(orders[I]==oo)[0] + # plot(wavelengths[I][III],residuals[III],'.') #show() - print "\t\t\tStart Global culling with ", N_l, " number of lines" - while (cond): + print("\t\t\tStart Global culling with ", N_l, " number of lines") + while (cond): index_worst = np.argmax( np.absolute(residuals) ) I.pop( index_worst ) N_l -= 1 @@ -2368,54 +2372,54 @@ def fitfunc_cheb(p,p_ref,chebs,m): residuals = errfunc(p1, p_ref, pix_centers[I], orders[I] + order0, wavelengths[I]) residuals_ms = 299792458.0 * residuals / wavelengths[I] - rms_ms = np.sqrt( np.var( residuals_ms ) ) + rms_ms = np.sqrt( np.var( residuals_ms ) ) #print 'p1',(1e-6*p1)*299792458.0 L = np.where( np.absolute(residuals_ms) > 4.*rms_ms ) - if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): + if ( (len(L[0]) == 0) and (rms_ms < maxrms) ) or (N_l < minlines): cond=0 #print "Eliminated line ", index_worst, " at order ", orders[index_worst] #print "RMS is ", rms_ms, " after elimination", len(L[0]), rms_ms, maxrms, N_l - print "\t\t\tFinal RMS is ", rms_ms - print "\t\t\tNumber of lines is ", N_l - print "\t\t\t--> Achievable RV precision is ", rms_ms/np.sqrt(N_l) - print "\t\t\tVelocity of ThAr w/r to solution provided is ", (1e-6*p1)*299792458.0 + print("\t\t\tFinal RMS is ", rms_ms) + print("\t\t\tNumber of lines is ", N_l) + print("\t\t\t--> Achievable RV precision is ", rms_ms/np.sqrt(N_l)) + print("\t\t\tVelocity of ThAr w/r to solution provided is ", (1e-6*p1)*299792458.0) #for oo in np.unique(orders[I]): - # III = np.where(orders[I]==oo)[0] - # plot(wavelengths[I][III],residuals[III],'.') + # III = np.where(orders[I]==oo)[0] + # plot(wavelengths[I][III],residuals[III],'.') #show() - return p1, pix_centers[I], orders[I], wavelengths[I], I, rms_ms, residuals + return p1, pix_centers[I], orders[I], wavelengths[I], I, rms_ms, residuals def Calculate_chebs(x,m, order0=89, ntotal=70,npix=2048.,Inverse=False,nx=5,nm=6): #normalize x medp = float(int(0.5*npix)) x_norm = (x-medp) / medp - + if (Inverse): # invert and normalize m - im = 1.0 / m + im = 1.0 / m im_max = 1.0 / float(order0) im_min = 1.0 / float(order0+ntotal) else: im = m im_max = float(order0+ntotal) im_min = float(order0) - + delta = 0.5*(im_max - im_min) - m_norm = (im-im_min-delta)/delta - + m_norm = (im-im_min-delta)/delta + coefs = [] for i in range(nx): - coefs.append(sp.chebyt(i+1)(x_norm)) + coefs.append(sp.chebyt(i+1)(x_norm)) for i in range(nm): - coefs.append(sp.chebyt(i+1)(m_norm)) + coefs.append(sp.chebyt(i+1)(m_norm)) """ u = sp.chebyt(1)(x_norm) u2 = sp.chebyt(2)(x_norm) u3 = sp.chebyt(3)(x_norm) - u4 = sp.chebyt(4)(x_norm) + u4 = sp.chebyt(4)(x_norm) v = sp.chebyt(1)(m_norm) v2 = sp.chebyt(2)(m_norm) v3 = sp.chebyt(3)(m_norm) @@ -2436,22 +2440,22 @@ def Joint_Polynomial_Cheby(p,chebs,nx,nm): ret_val = p[0] k=1 for i in range(nx): - ret_val += p[k]*xvec[i] - k+=1 + ret_val += p[k]*xvec[i] + k+=1 for i in range(nm): - ret_val += p[k]*mvec[i] - k+=1 + ret_val += p[k]*mvec[i] + k+=1 if nx >= nm: - for i in range(nx): - for j in range(min(nx-i,nm)): - ret_val += p[k]*xvec[i]*mvec[j] - k+=1 + for i in range(nx): + for j in range(min(nx-i,nm)): + ret_val += p[k]*xvec[i]*mvec[j] + k+=1 else: - for j in range(nm): - for i in range(min(nm-j-1,nx)): - ret_val += p[k]*xvec[i]*mvec[j] - k+=1 + for j in range(nm): + for i in range(min(nm-j-1,nx)): + ret_val += p[k]*xvec[i]*mvec[j] + k+=1 return ret_val def fp_base(f,n=3): @@ -2465,25 +2469,25 @@ def fp_base(f,n=3): I = np.where((f3*rms)[0] cond = True if len(I)==0: - cond = False + cond = False while cond: - #print I - iw = np.argmax(res**2) - xmin = np.delete(xmin,iw) - fmin = np.delete(fmin,iw) - coef = np.polyfit(xmin,fmin,n) - res = fmin - np.polyval(coef,xmin) - rms = np.sqrt(np.mean(res**2)) - I = np.where(res>3*rms)[0] - if len(I)==0: - cond = False + #print I + iw = np.argmax(res**2) + xmin = np.delete(xmin,iw) + fmin = np.delete(fmin,iw) + coef = np.polyfit(xmin,fmin,n) + res = fmin - np.polyval(coef,xmin) + rms = np.sqrt(np.mean(res**2)) + I = np.where(res>3*rms)[0] + if len(I)==0: + cond = False ox = np.arange(len(of)) tbase = np.polyval(coef,ox) @@ -2496,7 +2500,7 @@ def fp_base(f,n=3): def ccf_fp(fp,fpr,p1,order,order0=89,ntotal=70,npix=2048,Inv=True,nx=5,nm=6): def gaufp(p,v): - retval = np.exp(-((v-p[0])**2)/(2*p[1]**2)) + retval = np.exp(-((v-p[0])**2)/(2*p[1]**2)) return retval errgaufp = lambda p,c,v: np.ravel( (gaufp(p,v)-c) ) pix_centers = np.arange(len(fp)) @@ -2507,32 +2511,32 @@ def gaufp(p,v): vels = np.arange(-10000.,10000.,30.) ccf = [] for v in vels: - twav = wav*(1+v/299792458.) - tck = interpolate.splrep(twav,fp,k=3) - tfp = interpolate.splev(wav,tck)[500:-500] - tfp /= np.sum(tfp) - tfpr = fpr[500:-500] - tfpr /= np.sum(tfpr) - ccf.append(np.sum(tfpr*tfp)) + twav = wav*(1+v/299792458.) + tck = interpolate.splrep(twav,fp,k=3) + tfp = interpolate.splev(wav,tck)[500:-500] + tfp /= np.sum(tfp) + tfpr = fpr[500:-500] + tfpr /= np.sum(tfpr) + ccf.append(np.sum(tfpr*tfp)) ccf = np.array(ccf) try: - am = np.argmax(ccf) - imin = np.argmin(ccf[:am]) - rmin = am + np.argmin(ccf[am:]) - vels = vels[imin:rmin+1] - ccf = ccf[imin:rmin+1] - ccf -= .5*(ccf[0]+ccf[-1]) - ccf /= ccf.max() - - pg, success = scipy.optimize.leastsq(errgaufp, [0.,10.], args=(ccf, vels)) - - #plt.plot(vels,ccf) - #plt.plot(vels,gaufp(pg,vels)) - #plt.show() - return pg[0] + am = np.argmax(ccf) + imin = np.argmin(ccf[:am]) + rmin = am + np.argmin(ccf[am:]) + vels = vels[imin:rmin+1] + ccf = ccf[imin:rmin+1] + ccf -= .5*(ccf[0]+ccf[-1]) + ccf /= ccf.max() + + pg, success = scipy.optimize.leastsq(errgaufp, [0.,10.], args=(ccf, vels)) + + #plt.plot(vels,ccf) + #plt.plot(vels,gaufp(pg,vels)) + #plt.show() + return pg[0] except: - return -999. + return -999. def simbad_query_obname(obname): (th,tfile) = tempfile.mkstemp(prefix='CP', text=True) @@ -2548,17 +2552,17 @@ def simbad_query_obname(obname): values = [ ("scriptFIle", (pycurl.FORM_FILE, tfile)) ] - output = StringIO.StringIO() + output = StringIO() c = pycurl.Curl() - + c.setopt(pycurl.URL, "http://simbad.u-strasbg.fr/simbad/sim-script") c.setopt(c.HTTPPOST, values) - c.setopt(pycurl.WRITEFUNCTION, output.write) + c.setopt(pycurl.WRITEFUNCTION, output.write) c.perform() c.close() - result = output.getvalue() - lines = result.split('\n') + result = output.getvalue() + lines = result.split('\n') result = lines[len(lines)-3] query_success = False if (len(result) == 0): @@ -2571,7 +2575,7 @@ def simbad_query_obname(obname): sp_type_query = result.split('|')[2] else: query_success = False - sp_type_query = 'None' + sp_type_query = 'None' if not query_success: sp_type_query = None os.remove(tfile) @@ -2592,17 +2596,17 @@ def simbad_query_coords(ra,dec): values = [ ("scriptFIle", (pycurl.FORM_FILE, tfile)) ] - output = StringIO.StringIO() + output = StringIO() c = pycurl.Curl() - + c.setopt(pycurl.URL, "http://simbad.u-strasbg.fr/simbad/sim-script") c.setopt(c.HTTPPOST, values) - c.setopt(pycurl.WRITEFUNCTION, output.write) + c.setopt(pycurl.WRITEFUNCTION, output.write) c.perform() c.close() - - result = output.getvalue() - lines = result.split('\n') + + result = output.getvalue() + lines = result.split('\n') result = lines[len(lines)-3] query_success = False @@ -2610,18 +2614,18 @@ def simbad_query_coords(ra,dec): query_success = False else: query_success = True - try: - sp_type_query = result.split('|')[2] - except: - sp_type_query = 'None' - query_success = False + try: + sp_type_query = result.split('|')[2] + except: + sp_type_query = 'None' + query_success = False if not query_success: sp_type_query = 'None' os.remove(tfile) return query_success, sp_type_query def Lines_mBack(thar, sd, thres_rel=3, line_w=10): - """ + """ Given an extracted ThAr order, return a version where the background has been removed """ L = np.where(sd > 0) @@ -2636,38 +2640,38 @@ def Lines_mBack(thar, sd, thres_rel=3, line_w=10): # Now, mask these lines mask = np.ones( len(sd) ) for kk in lines: - mask[kk-line_w:kk+line_w+1] = 0 - + mask[kk-line_w:kk+line_w+1] = 0 + # New, final background estimnate X = np.array( range( len( d ) ) ) K = np.where((sd > 0) & (mask > 0)) if len(K[0])>0: - bkg = np.zeros( len(sd) ) + bkg = np.zeros( len(sd) ) bkg_T = lowess(thar[K].astype('double'), X[K],frac=0.2,it=3,return_sorted=False) tck1 = scipy.interpolate.splrep(X[K],bkg_T,k=1) bkg[L] = scipy.interpolate.splev(X[L],tck1) return bkg else: - return np.zeros( len(sd) ) + return np.zeros( len(sd) ) def FindLines_simple_sigma(d,sd,thres=3): """ Given an array, find lines above a sigma-threshold """ - L = np.where( (d > (thres*sd)) & (d > 0) ) + L = np.where( (d > (thres*sd)) & (d > 0) ) lines = [] for i in range(np.shape(L)[1]): j = L[0][i] if ((d[j] > d[j-1]) & (d[j-1] > d[j-2]) & (d[j] > d[j+1]) & (d[j+1] > d[j+2])): lines.append(j) - + return lines def XC_ThAr(thar_ref, thar_comp, pixel_span): """ Calculates the cross-correlation of two ThAr spectra """ - + I1 = np.where( thar_ref != 0 ) I2 = np.where( thar_comp != 0 ) x1 = np.maximum( np.min( I1 ), np.min( I2) ) + pixel_span @@ -2683,18 +2687,18 @@ def XC_ThAr(thar_ref, thar_comp, pixel_span): sd2 = np.sqrt( np.var( thar_comp[I2] ) ) norm = np.shape( thar_ref[I1] )[0] - + #print x1, x2 for k in range(N): - l1 = x1 + shifts[k] - l2 = x2 + shifts[k] + l1 = x1 + shifts[k] + l2 = x2 + shifts[k] XCor[k] = np.sum( (thar_ref[x1:x2] - mu1)* (thar_comp[l1:l2] - mu2) / (sd1 * sd2) ) / (norm-1) return XCor, shifts def XC_Gau_Fit(X,Y,back_lag=5, usemin=1): """ - Fits a Gaussian to a XC + Fits a Gaussian to a XC """ fitfunc = lambda p, x: p[0]*CorGaussian(x,p[1],p[2]) + p[3] @@ -2716,210 +2720,210 @@ def XC_Gau_Fit(X,Y,back_lag=5, usemin=1): return p1 def get_mask(sp_type_query,T_eff, query_success): - if (query_success): - if (sp_type_query.count('G') > 0): - print "Using G mask" - sp_type = 'G2' - elif (sp_type_query.count('K') > 0): - print "Using K mask" - sp_type = 'K5' - elif (sp_type_query.count('M') > 0): - print "Using M mask" - sp_type = 'K5' - elif (sp_type_query.count('F') > 0) or (sp_type_query.count('A') > 0): - print "Rather hot star, using G mask but this target is probably earlier than that.." - sp_type = 'G2' - else: - print "Probable problem, spectral type not in the books, using G-mask" - sp_type = 'G2' - else: - if (T_eff >= 4300) and (T_eff < 5350): - print "Using K mask" - sp_type = 'K5' - elif (T_eff >= 5350) and (T_eff < 6100): - print "Using G mask" - sp_type = 'G2' - elif (T_eff >= 6100): - print "Rather hot star, using G mask but this target is probably earlier than that.." - sp_type = 'G2' - else: - print "Using M mask" - sp_type = 'K5' - return sp_type + if (query_success): + if (sp_type_query.count('G') > 0): + print("Using G mask") + sp_type = 'G2' + elif (sp_type_query.count('K') > 0): + print("Using K mask") + sp_type = 'K5' + elif (sp_type_query.count('M') > 0): + print("Using M mask") + sp_type = 'K5' + elif (sp_type_query.count('F') > 0) or (sp_type_query.count('A') > 0): + print("Rather hot star, using G mask but this target is probably earlier than that..") + sp_type = 'G2' + else: + print("Probable problem, spectral type not in the books, using G-mask") + sp_type = 'G2' + else: + if (T_eff >= 4300) and (T_eff < 5350): + print("Using K mask") + sp_type = 'K5' + elif (T_eff >= 5350) and (T_eff < 6100): + print("Using G mask") + sp_type = 'G2' + elif (T_eff >= 6100): + print("Rather hot star, using G mask but this target is probably earlier than that..") + sp_type = 'G2' + else: + print("Using M mask") + sp_type = 'K5' + return sp_type def get_cont_single(W,F,E,nc=3,ll=3,lu=3,span=10,fact=3.,frac=0.3): - I = np.where(F==0)[0] - wav,flx,err = np.delete(W,I),np.delete(F,I),1./np.sqrt(np.delete(E,I)) - I = np.where(np.isnan(flx)==True)[0] - wav,flx,err = np.delete(wav,I),np.delete(flx,I),np.delete(err,I) - - i = 0 - rw,re,rd = [],[],[] - good_w,good_f = [],[] - cond = True - while cond: - while i < len(wav): - try: - w,f,e = wav[i:int(i+span)],flx[i:int(i+span)],err[i:int(i+span)] - dev = np.sqrt(np.var(f)) - rw.append(np.mean(w)) - re.append(np.median(e)) - rd.append(dev) - if dev0.5*np.median(flx): - good_w.append(w[int(0.5*span)]) - good_f.append(f[int(0.5*span)]) - except: - None - i+=1 - - #print len(good_w),0.1*len(wav),len(wav) - if len(good_w)>0.2*len(wav): - cond = False - else: - i = 0 - rw,re,rd = [],[],[] - good_w,good_f = [],[] - fact +=1 - if fact>20: - return np.array([0,np.max(scipy.signal.medfilt(F,21))]) - - gw,gf = np.array(good_w),np.array(good_f) - - rw,re,rd = np.array(rw),np.array(re),np.array(rd) - #plot(rw,rd,'ro') - #plot(rw,fact*re,'bo') - #show() - lori = len(gw) - cond = True - counter = 0 - while cond: - coef = np.polyfit(gw,gf,nc) - res = gf - np.polyval(coef,gw) - I = np.where(res>0)[0] - dev = np.mean(res[I]) - IO1 = np.where(res> lu*dev)[0] - IO2 = np.where(res<-ll*dev)[0] - IO = np.sort(np.hstack((IO1,IO2))) - if len(IO)==0 or len(gw)0.5*np.median(flx): + good_w.append(w[int(0.5*span)]) + good_f.append(f[int(0.5*span)]) + except: + None + i+=1 + + #print len(good_w),0.1*len(wav),len(wav) + if len(good_w)>0.2*len(wav): + cond = False + else: + i = 0 + rw,re,rd = [],[],[] + good_w,good_f = [],[] + fact +=1 + if fact>20: + return np.array([0,np.max(scipy.signal.medfilt(F,21))]) + + gw,gf = np.array(good_w),np.array(good_f) + + rw,re,rd = np.array(rw),np.array(re),np.array(rd) + #plot(rw,rd,'ro') + #plot(rw,fact*re,'bo') + #show() + lori = len(gw) + cond = True + counter = 0 + while cond: + coef = np.polyfit(gw,gf,nc) + res = gf - np.polyval(coef,gw) + I = np.where(res>0)[0] + dev = np.mean(res[I]) + IO1 = np.where(res> lu*dev)[0] + IO2 = np.where(res<-ll*dev)[0] + IO = np.sort(np.hstack((IO1,IO2))) + if len(IO)==0 or len(gw)=wav[0])[0] - - if len(I)>5: - J = np.where(wavlns[0])&(wavwi) & (wavW[i,0])&(wav0)[0] - dev = np.mean(res[IU]) - #plot(tw,tf) - #plot(tw, np.polyval(coef,tw)) - #plot(tw, np.polyval(coef,tw) +lu*dev) - #plot(tw, np.polyval(coef,tw) -ll*dev) - #show() - #print gfds - I = np.where((res-ll*dev))[0] - cond = True - - while cond: - tw,tf = tw[I],tf[I] - coef = np.polyfit(tw,tf,nc) - res = tf - np.polyval(coef,tw) - IU = np.where(res>0)[0] - dev = np.mean(res[IU]) - I = np.where((res-ll*dev))[0] - J1 = np.where(res>=lu*dev)[0] - J2 = np.where(res<=-ll*dev)[0] - - if (len(J1)==0 and len(J2)==0) or len(tw)=wav[0])[0] + + if len(I)>5: + J = np.where(wavlns[0])&(wavwi) & (wavW[i,0])&(wav0)[0] + dev = np.mean(res[IU]) + #plot(tw,tf) + #plot(tw, np.polyval(coef,tw)) + #plot(tw, np.polyval(coef,tw) +lu*dev) + #plot(tw, np.polyval(coef,tw) -ll*dev) + #show() + #print gfds + I = np.where((res-ll*dev))[0] + cond = True + + while cond: + tw,tf = tw[I],tf[I] + coef = np.polyfit(tw,tf,nc) + res = tf - np.polyval(coef,tw) + IU = np.where(res>0)[0] + dev = np.mean(res[IU]) + I = np.where((res-ll*dev))[0] + J1 = np.where(res>=lu*dev)[0] + J2 = np.where(res<=-ll*dev)[0] + + if (len(J1)==0 and len(J2)==0) or len(tw)= 1: - if xc_av[i] >= xc_av[i-1]: - lim_inf = i - break - i-=1 - - i = min_index - while i <= len(vels)-2: - if xc_av[i] >= xc_av[i+1]: - lim_sup = i - break - i+=1 - - if xc_av[lim_inf] < xc_av[lim_sup]: - xc_av = xc_av / xc_av[lim_inf] - else: - xc_av = xc_av / xc_av[lim_sup] - #print xc_av[lim_inf:min_index+1][::-1] - #print vels[lim_inf:min_index+1][::-1] - #plot(xc_av[lim_inf:min_index+1][::-1], vels[lim_inf:min_index+1][::-1]) - #show() - """ - finf = open('/home/rabrahm/testinf.txt','w') - for i in range(len(xc_av[lim_inf:min_index+1][::-1])): - finf.write(str(vels[lim_inf:min_index+1][::-1][i])+'\t'+str(xc_av[lim_inf:min_index+1][::-1][i])+'\n') - finf.close() - - fsup = open('/home/rabrahm/testsup.txt','w') - for i in range(len(xc_av[min_index:lim_sup+1])): - fsup.write(str(vels[min_index:lim_sup+1][i])+'\t'+str(xc_av[min_index:lim_sup+1][i])+'\n') - fsup.close() - """ - if len(xc_av[lim_inf:min_index+1][::-1]) <= 3 or len(xc_av[min_index:lim_sup+1])<=3: - stat = 0 - else: - stat = 1 - - if stat == 1: - Ii = np.argsort(xc_av[lim_inf:min_index+1]) - Is = np.argsort(xc_av[min_index:lim_sup+1]) - tck_inf = interpolate.splrep( xc_av[lim_inf:min_index+1][Ii], vels[lim_inf:min_index+1][Ii], k=3, s=0 ) - tck_sup = interpolate.splrep( xc_av[min_index:lim_sup+1][Is], vels[min_index:lim_sup+1][Is], k=3, s=0 ) - - if xc_av[lim_inf] < xc_av[lim_sup]: - xc_vec = np.arange(xc_av[min_index],xc_av[lim_inf],0.01) - else: - xc_vec = np.arange(xc_av[min_index],xc_av[lim_sup],0.01) - - interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=0) - interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=0) - der_interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=1) - der_interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=1) - #print interp_vels_inf - #print interp_vels_sup - bisect = 0.5 * (interp_vels_inf + interp_vels_sup) - - I_bottom = np.where((xc_vec > bot_i) & (xc_vec < bot_f))[0] - I_top = np.where((xc_vec > top_i) & (xc_vec < top_f))[0] - span = bisect[I_top].mean() - bisect[I_bottom].mean() - der_bottom = 0.5*(np.absolute(der_interp_vels_inf[I_bottom].mean()) + np.absolute(der_interp_vels_sup[I_bottom].mean())) - der_top = 0.5*(np.absolute(der_interp_vels_inf[I_top].mean()) + np.absolute(der_interp_vels_sup[I_top].mean())) - #print der_bottom, der_top - #print span - #plot(vels, xc_av) - #plot(bisect, xc_vec,'r.') - #show() - I2 = np.where( (xc_vec > 0.4) & (xc_vec < 0.81) )[0] - aju = np.polyfit(xc_vec[I2], bisect[I2],1) - slope = aju[0] - else: - span = -999.0 - der_bottom = 0 - der_top = 0 - slope = 0 - return span,der_bottom,der_top, slope, stat + #plot(vels, xc_av) + #show() + #dic = pickle.load(open('/data/echelle/coralie/red/20090908/CORALIE.2009-09-09T07:09:49.WASP-18_XC_G2.pkl','r')) + #dic = pickle.load(open('/data/echelle/coralie/red/20121110/CORALIE.2012-11-11T08:38:58.HD72673_XC_G2.pkl','r')) + #vels = dic['vels'] + #xc_av = dic['xc_av'] + + xc_av = xc_av - xc_av.min() + + min_index = np.argmin(xc_av) #index with near minimum of CCF + + lim_inf, lim_sup = 0, len(vels)-1 + + i = min_index + while i >= 1: + if xc_av[i] >= xc_av[i-1]: + lim_inf = i + break + i-=1 + + i = min_index + while i <= len(vels)-2: + if xc_av[i] >= xc_av[i+1]: + lim_sup = i + break + i+=1 + + if xc_av[lim_inf] < xc_av[lim_sup]: + xc_av = xc_av / xc_av[lim_inf] + else: + xc_av = xc_av / xc_av[lim_sup] + #print xc_av[lim_inf:min_index+1][::-1] + #print vels[lim_inf:min_index+1][::-1] + #plot(xc_av[lim_inf:min_index+1][::-1], vels[lim_inf:min_index+1][::-1]) + #show() + """ + finf = open('/home/rabrahm/testinf.txt','w') + for i in range(len(xc_av[lim_inf:min_index+1][::-1])): + finf.write(str(vels[lim_inf:min_index+1][::-1][i])+'\t'+str(xc_av[lim_inf:min_index+1][::-1][i])+'\n') + finf.close() + + fsup = open('/home/rabrahm/testsup.txt','w') + for i in range(len(xc_av[min_index:lim_sup+1])): + fsup.write(str(vels[min_index:lim_sup+1][i])+'\t'+str(xc_av[min_index:lim_sup+1][i])+'\n') + fsup.close() + """ + if len(xc_av[lim_inf:min_index+1][::-1]) <= 3 or len(xc_av[min_index:lim_sup+1])<=3: + stat = 0 + else: + stat = 1 + + if stat == 1: + Ii = np.argsort(xc_av[lim_inf:min_index+1]) + Is = np.argsort(xc_av[min_index:lim_sup+1]) + tck_inf = interpolate.splrep( xc_av[lim_inf:min_index+1][Ii], vels[lim_inf:min_index+1][Ii], k=3, s=0 ) + tck_sup = interpolate.splrep( xc_av[min_index:lim_sup+1][Is], vels[min_index:lim_sup+1][Is], k=3, s=0 ) + + if xc_av[lim_inf] < xc_av[lim_sup]: + xc_vec = np.arange(xc_av[min_index],xc_av[lim_inf],0.01) + else: + xc_vec = np.arange(xc_av[min_index],xc_av[lim_sup],0.01) + + interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=0) + interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=0) + der_interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=1) + der_interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=1) + #print interp_vels_inf + #print interp_vels_sup + bisect = 0.5 * (interp_vels_inf + interp_vels_sup) + + I_bottom = np.where((xc_vec > bot_i) & (xc_vec < bot_f))[0] + I_top = np.where((xc_vec > top_i) & (xc_vec < top_f))[0] + span = bisect[I_top].mean() - bisect[I_bottom].mean() + der_bottom = 0.5*(np.absolute(der_interp_vels_inf[I_bottom].mean()) + np.absolute(der_interp_vels_sup[I_bottom].mean())) + der_top = 0.5*(np.absolute(der_interp_vels_inf[I_top].mean()) + np.absolute(der_interp_vels_sup[I_top].mean())) + #print der_bottom, der_top + #print span + #plot(vels, xc_av) + #plot(bisect, xc_vec,'r.') + #show() + I2 = np.where( (xc_vec > 0.4) & (xc_vec < 0.81) )[0] + aju = np.polyfit(xc_vec[I2], bisect[I2],1) + slope = aju[0] + else: + span = -999.0 + der_bottom = 0 + der_top = 0 + slope = 0 + return span,der_bottom,der_top, slope, stat def calc_bss2(vels,xc,coef, bot_i=0.15, bot_f=0.4, top_i=0.6, top_f=0.9, dt=0.01): - try: - - I1 = np.where((vels>coef[1]-3*coef[2]) & (velscoef[1]) )[0] - I3 = np.where(velscoef[1]+4*coef[2])[0] - I = np.hstack((I3,I4)) - base = np.median(xc[I]) - - xc = base - xc - xc /= xc.max() - - - v1,x1 = vels[I1],xc[I1] - v2,x2 = vels[I2],xc[I2] - #plot(v1,x1) - #plot(v2,x2) - #show() - dp = top_f - vect = [] - while dp >= top_i: - lb = np.where(x1>dp)[0][0] - m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) - n = v1[lb] - m*x1[lb] - bs1 = m*dp+n - - lb = np.where(x2>dp)[0][-1] - m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) - n = v2[lb] - m*x2[lb] - bs2 = m*dp+n - vect.append(0.5*(bs2+bs1)) - dp-=dt - vect = np.array(vect) - - dp = bot_f - vecb = [] - while dp >= bot_i: - - lb = np.where(x1>dp)[0][0] - m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) - n = v1[lb] - m*x1[lb] - bs1 = m*dp+n - - lb = np.where(x2>dp)[0][-1] - m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) - n = v2[lb] - m*x2[lb] - bs2 = m*dp+n - vecb.append(0.5*(bs2+bs1)) - dp-=dt - vecb = np.array(vecb) - - return np.median(vecb) - np.median(vect) - except: - return -999.0 + try: + + I1 = np.where((vels>coef[1]-3*coef[2]) & (velscoef[1]) )[0] + I3 = np.where(velscoef[1]+4*coef[2])[0] + I = np.hstack((I3,I4)) + base = np.median(xc[I]) + + xc = base - xc + xc /= xc.max() + + + v1,x1 = vels[I1],xc[I1] + v2,x2 = vels[I2],xc[I2] + #plot(v1,x1) + #plot(v2,x2) + #show() + dp = top_f + vect = [] + while dp >= top_i: + lb = np.where(x1>dp)[0][0] + m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) + n = v1[lb] - m*x1[lb] + bs1 = m*dp+n + + lb = np.where(x2>dp)[0][-1] + m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) + n = v2[lb] - m*x2[lb] + bs2 = m*dp+n + vect.append(0.5*(bs2+bs1)) + dp-=dt + vect = np.array(vect) + + dp = bot_f + vecb = [] + while dp >= bot_i: + + lb = np.where(x1>dp)[0][0] + m = (v1[lb] - v1[lb-1])/(x1[lb]-x1[lb-1]) + n = v1[lb] - m*x1[lb] + bs1 = m*dp+n + + lb = np.where(x2>dp)[0][-1] + m = (v2[lb] - v2[lb+1])/(x2[lb]-x2[lb+1]) + n = v2[lb] - m*x2[lb] + bs2 = m*dp+n + vecb.append(0.5*(bs2+bs1)) + dp-=dt + vecb = np.array(vecb) + + return np.median(vecb) - np.median(vect) + except: + return -999.0 def gauss_samp(p,l,mu): - A,B,sig = p[0],p[1],p[2] - px = np.arange(len(l)).astype('float') - tck = interpolate.splrep(px,l,k=3) - pxs = np.arange(-0.5,len(l)-0.5,0.01) - I = np.where(pxs<0)[0] - pxs[I] *= -1. - I = np.where(pxs>=len(l)-1)[0] - temp = -np.arange(0.,0.5,0.01) - pxs[I] = temp + len(l) - 1 - ls = interpolate.splev(pxs,tck) - G = B + A * np.exp(-.5*((ls - mu)/sig)**2) - pxs = pxs.reshape((len(l),100)) - ls = ls.reshape((len(l),100)) - G = G.reshape((len(l),100)) - ret = np.mean(G,axis=1) - return ret + A,B,sig = p[0],p[1],p[2] + px = np.arange(len(l)).astype('float') + tck = interpolate.splrep(px,l,k=3) + pxs = np.arange(-0.5,len(l)-0.5,0.01) + I = np.where(pxs<0)[0] + pxs[I] *= -1. + I = np.where(pxs>=len(l)-1)[0] + temp = -np.arange(0.,0.5,0.01) + pxs[I] = temp + len(l) - 1 + ls = interpolate.splev(pxs,tck) + G = B + A * np.exp(-.5*((ls - mu)/sig)**2) + pxs = pxs.reshape((len(l),100)) + ls = ls.reshape((len(l),100)) + G = G.reshape((len(l),100)) + ret = np.mean(G,axis=1) + return ret def err_samp(p,y,x,mu): - return np.ravel( (gauss_samp(p,x,mu) - y) ) + return np.ravel( (gauss_samp(p,x,mu) - y) ) def convolve(wav,flx,R): devs = wav/(2.35482*R) @@ -3126,166 +3130,166 @@ def convolve(wav,flx,R): NF = [] for i in range(len(wav)): I = np.where((wav>lims1[i]) & (wav= 1: - if xc_av[i] >= xc_av[i-1]: - lim_inf = i - break - i-=1 - - i = min_index - while i <= len(vels)-2: - if xc_av[i] >= xc_av[i+1]: - lim_sup = i - break - i+=1 - - if xc_av[lim_inf] < xc_av[lim_sup]: - xc_av = xc_av / xc_av[lim_inf] - else: - xc_av = xc_av / xc_av[lim_sup] - #print xc_av[lim_inf:min_index+1][::-1] - #print vels[lim_inf:min_index+1][::-1] - #plot(xc_av[lim_inf:min_index+1][::-1], vels[lim_inf:min_index+1][::-1]) - #show() - """ - finf = open('/home/rabrahm/testinf.txt','w') - for i in range(len(xc_av[lim_inf:min_index+1][::-1])): - finf.write(str(vels[lim_inf:min_index+1][::-1][i])+'\t'+str(xc_av[lim_inf:min_index+1][::-1][i])+'\n') - finf.close() - - fsup = open('/home/rabrahm/testsup.txt','w') - for i in range(len(xc_av[min_index:lim_sup+1])): - fsup.write(str(vels[min_index:lim_sup+1][i])+'\t'+str(xc_av[min_index:lim_sup+1][i])+'\n') - fsup.close() - """ - if len(xc_av[lim_inf:min_index+1][::-1]) <= 3 or len(xc_av[min_index:lim_sup+1])<=3: - stat = 0 - else: - stat = 1 - - if stat == 1: - Ii = np.argsort(xc_av[lim_inf:min_index+1]) - Is = np.argsort(xc_av[min_index:lim_sup+1]) - tck_inf = interpolate.splrep( xc_av[lim_inf:min_index+1][Ii], vels[lim_inf:min_index+1][Ii], k=3, s=0 ) - tck_sup = interpolate.splrep( xc_av[min_index:lim_sup+1][Is], vels[min_index:lim_sup+1][Is], k=3, s=0 ) - - if xc_av[lim_inf] < xc_av[lim_sup]: - xc_vec = np.arange(xc_av[min_index],xc_av[lim_inf],0.01) - else: - xc_vec = np.arange(xc_av[min_index],xc_av[lim_sup],0.01) - - interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=0) - interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=0) - der_interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=1) - der_interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=1) - #print interp_vels_inf - #print interp_vels_sup - bisect = 0.5 * (interp_vels_inf + interp_vels_sup) - - I_bottom = np.where((xc_vec > bot_i) & (xc_vec < bot_f))[0] - I_top = np.where((xc_vec > top_i) & (xc_vec < top_f))[0] - span = bisect[I_top].mean() - bisect[I_bottom].mean() - der_bottom = 0.5*(np.absolute(der_interp_vels_inf[I_bottom].mean()) + np.absolute(der_interp_vels_sup[I_bottom].mean())) - der_top = 0.5*(np.absolute(der_interp_vels_inf[I_top].mean()) + np.absolute(der_interp_vels_sup[I_top].mean())) - #print der_bottom, der_top - #print span - #plot(vels, xc_av) - #plot(bisect, xc_vec,'r.') - #show() - I2 = np.where( (xc_vec > 0.4) & (xc_vec < 0.81) )[0] - aju = np.polyfit(xc_vec[I2], bisect[I2],1) - slope = aju[0] - else: - span = -999.0 - der_bottom = 0 - der_top = 0 - slope = 0 - return span,der_bottom,der_top, slope, stat + + xc_av = xc_av - xc_av.min() + + min_index = np.argmin(xc_av) #index with near minimum of CCF + + lim_inf, lim_sup = 0, len(vels)-1 + + i = min_index + while i >= 1: + if xc_av[i] >= xc_av[i-1]: + lim_inf = i + break + i-=1 + + i = min_index + while i <= len(vels)-2: + if xc_av[i] >= xc_av[i+1]: + lim_sup = i + break + i+=1 + + if xc_av[lim_inf] < xc_av[lim_sup]: + xc_av = xc_av / xc_av[lim_inf] + else: + xc_av = xc_av / xc_av[lim_sup] + #print xc_av[lim_inf:min_index+1][::-1] + #print vels[lim_inf:min_index+1][::-1] + #plot(xc_av[lim_inf:min_index+1][::-1], vels[lim_inf:min_index+1][::-1]) + #show() + """ + finf = open('/home/rabrahm/testinf.txt','w') + for i in range(len(xc_av[lim_inf:min_index+1][::-1])): + finf.write(str(vels[lim_inf:min_index+1][::-1][i])+'\t'+str(xc_av[lim_inf:min_index+1][::-1][i])+'\n') + finf.close() + + fsup = open('/home/rabrahm/testsup.txt','w') + for i in range(len(xc_av[min_index:lim_sup+1])): + fsup.write(str(vels[min_index:lim_sup+1][i])+'\t'+str(xc_av[min_index:lim_sup+1][i])+'\n') + fsup.close() + """ + if len(xc_av[lim_inf:min_index+1][::-1]) <= 3 or len(xc_av[min_index:lim_sup+1])<=3: + stat = 0 + else: + stat = 1 + + if stat == 1: + Ii = np.argsort(xc_av[lim_inf:min_index+1]) + Is = np.argsort(xc_av[min_index:lim_sup+1]) + tck_inf = interpolate.splrep( xc_av[lim_inf:min_index+1][Ii], vels[lim_inf:min_index+1][Ii], k=3, s=0 ) + tck_sup = interpolate.splrep( xc_av[min_index:lim_sup+1][Is], vels[min_index:lim_sup+1][Is], k=3, s=0 ) + + if xc_av[lim_inf] < xc_av[lim_sup]: + xc_vec = np.arange(xc_av[min_index],xc_av[lim_inf],0.01) + else: + xc_vec = np.arange(xc_av[min_index],xc_av[lim_sup],0.01) + + interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=0) + interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=0) + der_interp_vels_inf = interpolate.splev(xc_vec, tck_inf, der=1) + der_interp_vels_sup = interpolate.splev(xc_vec, tck_sup, der=1) + #print interp_vels_inf + #print interp_vels_sup + bisect = 0.5 * (interp_vels_inf + interp_vels_sup) + + I_bottom = np.where((xc_vec > bot_i) & (xc_vec < bot_f))[0] + I_top = np.where((xc_vec > top_i) & (xc_vec < top_f))[0] + span = bisect[I_top].mean() - bisect[I_bottom].mean() + der_bottom = 0.5*(np.absolute(der_interp_vels_inf[I_bottom].mean()) + np.absolute(der_interp_vels_sup[I_bottom].mean())) + der_top = 0.5*(np.absolute(der_interp_vels_inf[I_top].mean()) + np.absolute(der_interp_vels_sup[I_top].mean())) + #print der_bottom, der_top + #print span + #plot(vels, xc_av) + #plot(bisect, xc_vec,'r.') + #show() + I2 = np.where( (xc_vec > 0.4) & (xc_vec < 0.81) )[0] + aju = np.polyfit(xc_vec[I2], bisect[I2],1) + slope = aju[0] + else: + span = -999.0 + der_bottom = 0 + der_top = 0 + slope = 0 + return span,der_bottom,der_top, slope, stat def cor_thar(spec, span=10, filename='/data/echelle/ecpipe/DuPont/wavcals/',binning=1, di=0.1): - - f = open(filename).readlines() - - pixel_centers_0 = [] - - for line in f: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])/binning) - - ml = array(pixel_centers_0) - 1 - mh = array(pixel_centers_0) + 1 - - vi = -span - vf = span - - NM = np.sqrt(np.add.reduce((mh - ml)**2)) - NS = np.sqrt(integrate.simps(spec*spec) ) - - MP = np.arange(0,len(spec)-1+di,di) - MV = np.zeros(len(MP)) - - for lin in range(len(ml)): - I = np.where((MP >= ml[lin]) & (MP <= mh[lin]))[0] - MV[I] = 1.0 - - tck = interpolate.splrep(np.arange(len(spec)),spec,k=3,s=0) - SV = interpolate.splev(MP,tck,der=0) - - CCF = [] - DEL = [] - - while vi <= vf: - mp = MP.copy() + vi - - mv = MV.copy() - - I = np.where(mp < 0.0)[0] - - if len(I) > 0: - - comp = mv[I] - mp = np.delete(mp,I) - mv = np.delete(mv,I) - mv = np.hstack([mv,comp]) - I = np.where(mp > float(len(spec)-1))[0] - - if len(I) > 0: - - comp = mv[I] - mp = np.delete(mp,I) - mv = np.delete(mv,I) - mv = np.hstack([comp,mv]) - CCF.append(np.add.reduce(SV*mv)*di/(NM*NS)) - DEL.append(vi) - - vi = vi + di - - CCF = np.array(CCF) - DEL = np.array(DEL) - #plot(DEL,CCF,'ro') - #show() - I = np.argmax(CCF) - #plot(DEL,CCF) - return CCF[I],DEL[I] + + f = open(filename).readlines() + + pixel_centers_0 = [] + + for line in f: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])/binning) + + ml = array(pixel_centers_0) - 1 + mh = array(pixel_centers_0) + 1 + + vi = -span + vf = span + + NM = np.sqrt(np.add.reduce((mh - ml)**2)) + NS = np.sqrt(integrate.simps(spec*spec) ) + + MP = np.arange(0,len(spec)-1+di,di) + MV = np.zeros(len(MP)) + + for lin in range(len(ml)): + I = np.where((MP >= ml[lin]) & (MP <= mh[lin]))[0] + MV[I] = 1.0 + + tck = interpolate.splrep(np.arange(len(spec)),spec,k=3,s=0) + SV = interpolate.splev(MP,tck,der=0) + + CCF = [] + DEL = [] + + while vi <= vf: + mp = MP.copy() + vi + + mv = MV.copy() + + I = np.where(mp < 0.0)[0] + + if len(I) > 0: + + comp = mv[I] + mp = np.delete(mp,I) + mv = np.delete(mv,I) + mv = np.hstack([mv,comp]) + I = np.where(mp > float(len(spec)-1))[0] + + if len(I) > 0: + + comp = mv[I] + mp = np.delete(mp,I) + mv = np.delete(mv,I) + mv = np.hstack([comp,mv]) + CCF.append(np.add.reduce(SV*mv)*di/(NM*NS)) + DEL.append(vi) + + vi = vi + di + + CCF = np.array(CCF) + DEL = np.array(DEL) + #plot(DEL,CCF,'ro') + #show() + I = np.argmax(CCF) + #plot(DEL,CCF) + return CCF[I],DEL[I] def plot_CCF(xc_dict,moon_dict,path='XC.pdf'): @@ -3328,7 +3332,7 @@ def plot_CCF(xc_dict,moon_dict,path='XC.pdf'): ax1.plot(vels[Ls2], XCmodelgau,'r-',label='Gaussian fit') if moonmatters: ax1.plot(vels[Ls2_m], XCmodelgau_m,'g-',label='Double Gaussian fit') - + if refvel > vels[0] and refvel < vels[-1]: ax1.axvline(refvel,linestyle='--',color='k', label = 'RV Moon') ax1.text(vels[0], xc_av.min(), moon_state + ' moon:\nseparation: '+str(round(moonsep,1))+' deg\nillumination: '+str(int(round(lunation,2)*100))+' %\naltitude: '+ str(mephem.alt)+'\nTexp = '+str(int(round(texp))) +' s', style='italic',fontsize=7) @@ -3354,17 +3358,17 @@ def get_disp(obname,reffile='reffile.txt'): f = open(reffile,'r') lines = f.readlines() f.close() - found = False + found = False for line in lines: cos = line.split(',') if cos[0] == obname: - disp = float(cos[7]) - found = True - break - if not found: - print '\t\tWarning! There is no predefined dispersion of the CCF.' + disp = float(cos[7]) + found = True + break + if not found: + print('\t\tWarning! There is no predefined dispersion of the CCF.') except: - print '\t\tWarning! There is no predefined dispersion of the CCF.' + print('\t\tWarning! There is no predefined dispersion of the CCF.') return disp @@ -3379,53 +3383,53 @@ def get_mask_reffile(obname,reffile='reffile.txt',base='../../xc_masks/'): lines = f.readlines() f.close() - found = False + found = False for line in lines: cos = line.split(',') - msk = cos[6].strip() + msk = cos[6].strip() if cos[0] == obname and (msk=='G2' or msk=='K5' or msk=='M2'): - sp_type = cos[6][:2] - found = True - break - if not found: - print '\t\tWarning! Target not found in reference mask file. Using default mask (G2)' + sp_type = cos[6][:2] + found = True + break + if not found: + print('\t\tWarning! Target not found in reference mask file. Using default mask (G2)') except: - print '\t\tWarning! Problem with reference mask file. Forcing to G2 mask' + print('\t\tWarning! Problem with reference mask file. Forcing to G2 mask') if sp_type == 'G2': - xc_mask = xc_masks[0] + xc_mask = xc_masks[0] elif sp_type == 'K5': - xc_mask = xc_masks[1] + xc_mask = xc_masks[1] else: xc_mask = xc_masks[2] return sp_type, xc_mask - + def get_mask_query(sp_type_query,base='../../xc_masks/'): xc_masks = [base+'G2.mas',\ base+'K5.mas',\ base+'M2.mas' ] if (sp_type_query.count('G') > 0): - print "Using G mask according to simbad" + print("Using G mask according to simbad") mask = xc_masks[0] sp_type = 'G2' elif (sp_type_query.count('K') > 0): - print "Using K mask according to simbad" + print("Using K mask according to simbad") mask = xc_masks[1] sp_type = 'K5' elif (sp_type_query.count('M') > 0): - print "Using M mask according to simbad" + print("Using M mask according to simbad") mask = xc_masks[2] sp_type = 'M5' elif (sp_type_query.count('F') > 0) or (sp_type_query.count('A') > 0): - print "Rather hot star according to simbad, using G mask but this target is probably earlier than that.." + print("Rather hot star according to simbad, using G mask but this target is probably earlier than that..") mask = xc_masks[0] sp_type = 'G2' else: - print "Probable problem, spectral type not in the books according to simbad, using G-mask" - mask = xc_masks[0] - sp_type = 'G2' + print("Probable problem, spectral type not in the books according to simbad, using G-mask") + mask = xc_masks[0] + sp_type = 'G2' return sp_type, mask def get_mask_teff(T_eff,base='../../xc_masks/'): @@ -3433,364 +3437,364 @@ def get_mask_teff(T_eff,base='../../xc_masks/'): base+'K5.mas',\ base+'M2.mas' ] if (T_eff >= 4300) and (T_eff < 5350): - print "Using K mask according to T_eff" + print("Using K mask according to T_eff") mask = xc_masks[1] sp_type = 'K5' elif (T_eff >= 5350) and (T_eff < 6100): - print "Using G mask according to T_eff" + print("Using G mask according to T_eff") mask = xc_masks[0] sp_type = 'G2' elif (T_eff >= 6100): - print "Rather hot star according to T_eff, using G mask but this target is probably earlier than that.." + print("Rather hot star according to T_eff, using G mask but this target is probably earlier than that..") mask = xc_masks[0] sp_type = 'G2' else: - print "Using M mask according to T_eff" + print("Using M mask according to T_eff") mask = xc_masks[2] sp_type = 'M5' return sp_type, mask def iau_cal2jd(IY,IM,ID): - IYMIN = -4799. - MTAB = np.array([ 31., 28., 31., 30., 31., 30., 31., 31., 30., 31., 30., 31.]) - J = 0 - if IY < IYMIN: - J = -1 - else: - if IM>=1 and IM <= 12: - if IY%4 == 0: - MTAB[1] = 29. - else: - MTAB[1] = 28. - - if IY%100 == 0 and IY%400!=0: - MTAB[1] = 28. - if ID < 1 or ID > MTAB[IM-1]: - J = -3 - a = ( 14 - IM ) / 12 - y = IY + 4800 - a - m = IM + 12*a -3 - DJM0 = 2400000.5 - DJM = ID + (153*m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045 - 2400001. - else: - J = -2 - return DJM0, DJM, J + IYMIN = -4799. + MTAB = np.array([ 31., 28., 31., 30., 31., 30., 31., 31., 30., 31., 30., 31.]) + J = 0 + if IY < IYMIN: + J = -1 + else: + if IM>=1 and IM <= 12: + if IY%4 == 0: + MTAB[1] = 29. + else: + MTAB[1] = 28. + + if IY%100 == 0 and IY%400!=0: + MTAB[1] = 28. + if ID < 1 or ID > MTAB[IM-1]: + J = -3 + a = ( 14 - IM ) / 12 + y = IY + 4800 - a + m = IM + 12*a -3 + DJM0 = 2400000.5 + DJM = ID + (153*m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045 - 2400001. + else: + J = -2 + return DJM0, DJM, J def identify_order(thar, path, window=100,di=0.5): - ccfs,shifts,ors = [],[],[] - for o in range(len(thar)): - tharo = thar[o] - scipy.signal.medfilt(thar[o],101) - ccf_max, shift = cor_thar(tharo,filename=path,span=window,di=di) - ccfs.append(ccf_max) - shifts.append(shift) - ors.append(o) - ccfs,shifts,ors = np.array(ccfs),np.array(shifts),np.array(ors) - im = np.argmax(ccfs) - return ors[im],shifts[im] - + ccfs,shifts,ors = [],[],[] + for o in range(len(thar)): + tharo = thar[o] - scipy.signal.medfilt(thar[o],101) + ccf_max, shift = cor_thar(tharo,filename=path,span=window,di=di) + ccfs.append(ccf_max) + shifts.append(shift) + ors.append(o) + ccfs,shifts,ors = np.array(ccfs),np.array(shifts),np.array(ors) + im = np.argmax(ccfs) + return ors[im],shifts[im] + def simbad_coords(obname,mjd): - if obname.lower() == 'alphacent': - obname = 'alpha cent' - elif obname.lower() == 'alphaboo': - obname = 'alpha boo' - elif obname.lower() == 'hadar' or obname.lower() == 'betacen': - obname = 'beta cen' - elif obname.lower() == 'diphda': - obname = 'bet cet' - elif obname.lower() == 'betacar': - obname = 'beta car' - elif obname.lower() == 'betscl': - obname = 'bet scl' - elif obname.lower() == 'bvel': - obname = 'b vel' - elif obname.lower() == 'deltasco': - obname = 'del sco' - elif obname.lower() == 'delcen': - obname = 'del cen' - elif obname.lower() == 'epsilonaqr': - obname = 'eps aqr' - elif obname.lower() == 'epspsa': - obname = 'eps psa' - elif obname.lower() == 'etahya' or obname.lower() == 'ethahydra': - obname = 'eta Hya' - elif obname.lower() == 'etapsa': - obname = 'eta psa' - elif obname.lower() == 'etacen': - obname = 'eta cen' - elif obname.lower() == 'opup': - obname = 'o Pup' - elif obname.lower() == 'etacar': - obname = 'eta Car' - elif obname.lower() == 'agcar': - obname = 'ag Car' - elif obname.lower() == 'hrcar': - obname = 'hr Car' - elif obname.lower() == 'sslep': - obname = 'ss lep' - elif obname.lower() == 'thetavir': - obname = 'theta vir' - elif obname.lower() == 'mucen': - obname = 'mu cen' - elif obname.lower() == 'lesath': - obname = 'ups sco' - elif obname.lower() == 'mulup': - obname = 'mu lup' - elif obname.lower() == 'chioph': - obname = 'chi oph' - elif obname.lower() == 'dlup': - obname = 'd lup' - elif obname.lower() == '48lib': - obname = '48 lib' - elif obname.lower() == 'iotara': - obname = 'iot ara' - elif obname.lower() == 'qvtel': - obname = 'qv tel' - elif obname.lower() == 'taucet': - obname = 'tau cet' - elif obname.lower() == 'pi2ori': - obname = 'pi2 ori' - elif obname.lower() == 'zetapeg': - obname = 'zet peg' - elif obname.lower() == 'tpyx': - obname = 't pyx' - elif obname.lower() == 'omicronpup': - obname = 'omi pup' - - paths = ['http://simbad.u-strasbg.fr/simbad/sim-script','http://simbad.harvard.edu/simbad/sim-script'] - - cond = True - i = 0 - while cond: - try: - sp,ra,dec = 0,0,0 - (th,tfile) = tempfile.mkstemp(prefix='CP', text=True) - tf = open(tfile,'w') - tf.write("output console=off\n") - tf.write("output script=off\n") - tf.write("output error=merge\n") - tf.write("set limit 1\n") - tf.write("format object fmt1 \"%IDLIST(1) | %OTYPELIST(S) | %SP(S) | %COO(A) | %COO(D) | %PM(A) | %PM(D)\"\n") - tf.write("result full\n") - tf.write("query id %s\n" % ( obname ) ) - tf.close() - values = [("scriptFIle", (pycurl.FORM_FILE, tfile))] - output = StringIO.StringIO() - c = pycurl.Curl() - c.setopt(pycurl.URL, paths[i]) - c.setopt(c.HTTPPOST, values) - c.setopt(pycurl.WRITEFUNCTION, output.write) - c.perform() - except: - if i == 0: - i = 1 - else: - i=0 - print 'Trying again to perform query to SIMBAD, changing to', paths[i] - - else: - cond = False - c.close() - result = output.getvalue() - lines = result.split('\n') - info = lines[6].split('|') - print info - if 'Unrecogniezd' in info[0] or 'not' in info[0]: - know = False - else: - know = True - sp,ra,dec,pmra,pmdec = info[2],info[3],info[4],info[5],info[6] - - if '~' in pmra: - pmra = '0.' - if '~' in pmdec: - pmdec = '0.' - - rad = ra.split() - decd = dec.split() - ra = float(rad[0])*360./24. + float(rad[1])*6./24. + float(rad[2])/240. + (float(pmra)/(3600*1000.))*((mjd-51544.5)/365.) - if float(decd[0])<0: - dec = -(np.absolute(float(decd[0])) + float(decd[1])/60. + float(decd[2])/3600.) + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) - else: - dec = float(decd[0]) + float(decd[1])/60. + float(decd[2])/3600. + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) - return sp,ra,dec,know + if obname.lower() == 'alphacent': + obname = 'alpha cent' + elif obname.lower() == 'alphaboo': + obname = 'alpha boo' + elif obname.lower() == 'hadar' or obname.lower() == 'betacen': + obname = 'beta cen' + elif obname.lower() == 'diphda': + obname = 'bet cet' + elif obname.lower() == 'betacar': + obname = 'beta car' + elif obname.lower() == 'betscl': + obname = 'bet scl' + elif obname.lower() == 'bvel': + obname = 'b vel' + elif obname.lower() == 'deltasco': + obname = 'del sco' + elif obname.lower() == 'delcen': + obname = 'del cen' + elif obname.lower() == 'epsilonaqr': + obname = 'eps aqr' + elif obname.lower() == 'epspsa': + obname = 'eps psa' + elif obname.lower() == 'etahya' or obname.lower() == 'ethahydra': + obname = 'eta Hya' + elif obname.lower() == 'etapsa': + obname = 'eta psa' + elif obname.lower() == 'etacen': + obname = 'eta cen' + elif obname.lower() == 'opup': + obname = 'o Pup' + elif obname.lower() == 'etacar': + obname = 'eta Car' + elif obname.lower() == 'agcar': + obname = 'ag Car' + elif obname.lower() == 'hrcar': + obname = 'hr Car' + elif obname.lower() == 'sslep': + obname = 'ss lep' + elif obname.lower() == 'thetavir': + obname = 'theta vir' + elif obname.lower() == 'mucen': + obname = 'mu cen' + elif obname.lower() == 'lesath': + obname = 'ups sco' + elif obname.lower() == 'mulup': + obname = 'mu lup' + elif obname.lower() == 'chioph': + obname = 'chi oph' + elif obname.lower() == 'dlup': + obname = 'd lup' + elif obname.lower() == '48lib': + obname = '48 lib' + elif obname.lower() == 'iotara': + obname = 'iot ara' + elif obname.lower() == 'qvtel': + obname = 'qv tel' + elif obname.lower() == 'taucet': + obname = 'tau cet' + elif obname.lower() == 'pi2ori': + obname = 'pi2 ori' + elif obname.lower() == 'zetapeg': + obname = 'zet peg' + elif obname.lower() == 'tpyx': + obname = 't pyx' + elif obname.lower() == 'omicronpup': + obname = 'omi pup' + + paths = ['http://simbad.u-strasbg.fr/simbad/sim-script','http://simbad.harvard.edu/simbad/sim-script'] + + cond = True + i = 0 + while cond: + try: + sp,ra,dec = 0,0,0 + (th,tfile) = tempfile.mkstemp(prefix='CP', text=True) + tf = open(tfile,'w') + tf.write("output console=off\n") + tf.write("output script=off\n") + tf.write("output error=merge\n") + tf.write("set limit 1\n") + tf.write("format object fmt1 \"%IDLIST(1) | %OTYPELIST(S) | %SP(S) | %COO(A) | %COO(D) | %PM(A) | %PM(D)\"\n") + tf.write("result full\n") + tf.write("query id %s\n" % ( obname ) ) + tf.close() + values = [("scriptFIle", (pycurl.FORM_FILE, tfile))] + output = StringIO() + c = pycurl.Curl() + c.setopt(pycurl.URL, paths[i]) + c.setopt(c.HTTPPOST, values) + c.setopt(pycurl.WRITEFUNCTION, output.write) + c.perform() + except: + if i == 0: + i = 1 + else: + i=0 + print('Trying again to perform query to SIMBAD, changing to', paths[i]) + + else: + cond = False + c.close() + result = output.getvalue() + lines = result.split('\n') + info = lines[6].split('|') + print(info) + if 'Unrecogniezd' in info[0] or 'not' in info[0]: + know = False + else: + know = True + sp,ra,dec,pmra,pmdec = info[2],info[3],info[4],info[5],info[6] + + if '~' in pmra: + pmra = '0.' + if '~' in pmdec: + pmdec = '0.' + + rad = ra.split() + decd = dec.split() + ra = float(rad[0])*360./24. + float(rad[1])*6./24. + float(rad[2])/240. + (float(pmra)/(3600*1000.))*((mjd-51544.5)/365.) + if float(decd[0])<0: + dec = -(np.absolute(float(decd[0])) + float(decd[1])/60. + float(decd[2])/3600.) + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) + else: + dec = float(decd[0]) + float(decd[1])/60. + float(decd[2])/3600. + (float(pmdec)/(3600*1000.))*((mjd-51544.5)/365.) + return sp,ra,dec,know def spec_ccf(sw,sf,mw,mf,vi,vf,dv): - mf = mf -1 - mf = -mf - tck = interpolate.splrep(mw,mf,k=1) - v = vi - retccf = [] - vels = [] - while v<=vf: - swt = sw * (1 + v/299792.458) - mft = interpolate.splev(swt,tck) - mft -= np.mean(mft) - sft = sf - np.mean(sf) - #sft = sf.copy() - retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) - vels.append(v) - v+=dv - return np.array(vels),np.array(retccf) + mf = mf -1 + mf = -mf + tck = interpolate.splrep(mw,mf,k=1) + v = vi + retccf = [] + vels = [] + while v<=vf: + swt = sw * (1 + v/299792.458) + mft = interpolate.splev(swt,tck) + mft -= np.mean(mft) + sft = sf - np.mean(sf) + #sft = sf.copy() + retccf.append(np.sum(mft*sft)/np.sqrt(np.sum(mft**2)*np.sum(sft**2))) + vels.append(v) + v+=dv + return np.array(vels),np.array(retccf) def RVforFR(data,teff=6750,logg=4.5,feh=-1.0,vsini=40.,model_path='../data/COELHO_MODELS/R_40000b/'): - def fitfunc(p,x): - ret = p[3] + p[0] * np.exp(-.5*((x-p[1])/p[2])**2) - return ret - errfunc = lambda p,x,y: np.ravel( (fitfunc(p,x)-y) ) - - if feh == -1: - sfeh = 'm10' - elif feh == -0.5: - sfeh = 'm05' - elif feh == 0: - sfeh = 'p00' - else: - sfeh = 'p05' - - - model = model_path+'/vsini_0.0/R_0.0_'+str(int(teff))+'_'+str(int(logg*10))+'_'+sfeh+'p00.ms.fits' - sc = pyfits.getdata(model) - hd = pyfits.getheader(model) - mw = np.arange(len(sc))*hd['CDELT1']+hd['CRVAL1'] - #"""" - I = np.where((mw>6520)&(mw<6600))[0] - sc[I] = 1. - I = np.where((mw>5888)&(mw<5897))[0] - sc[I] = 1. - I = np.where((mw>4310)&(mw<4360))[0] - sc[I] = 1. - I = np.where((mw>4070)&(mw<4130))[0] - sc[I] = 1. - I = np.where((mw>3875)&(mw<3900))[0] - sc[I] = 1. - I = np.where((mw>3920)&(mw<3945))[0] - sc[I] = 1. - I = np.where((mw>3955)&(mw<3980))[0] - sc[I] = 1. - I = np.where(mw<3850)[0] - sc[I] = 1. - #""" - - mw = ToVacuum(mw) - ccftot = [] - - for i in range(data.shape[1]): - scf = data[5,i] - scw = data[0,i] - J = np.where(scf!=0)[0] - scw,scf = scw[J],scf[J] - I = np.where((mw>scw[0]-100) & (mw6520)&(mw<6600))[0] + sc[I] = 1. + I = np.where((mw>5888)&(mw<5897))[0] + sc[I] = 1. + I = np.where((mw>4310)&(mw<4360))[0] + sc[I] = 1. + I = np.where((mw>4070)&(mw<4130))[0] + sc[I] = 1. + I = np.where((mw>3875)&(mw<3900))[0] + sc[I] = 1. + I = np.where((mw>3920)&(mw<3945))[0] + sc[I] = 1. + I = np.where((mw>3955)&(mw<3980))[0] + sc[I] = 1. + I = np.where(mw<3850)[0] + sc[I] = 1. + #""" + + mw = ToVacuum(mw) + ccftot = [] + + for i in range(data.shape[1]): + scf = data[5,i] + scw = data[0,i] + J = np.where(scf!=0)[0] + scw,scf = scw[J],scf[J] + I = np.where((mw>scw[0]-100) & (mw6520)&(mw<6600))[0] - sc[I] = 1. - I = np.where((mw>5888)&(mw<5897))[0] - sc[I] = 1. - I = np.where((mw>4310)&(mw<4360))[0] - sc[I] = 1. - I = np.where((mw>4070)&(mw<4130))[0] - sc[I] = 1. - I = np.where((mw>3875)&(mw<3900))[0] - sc[I] = 1. - I = np.where((mw>3920)&(mw<3945))[0] - sc[I] = 1. - I = np.where((mw>3955)&(mw<3980))[0] - sc[I] = 1. - I = np.where(mw<3850)[0] - sc[I] = 1. - - mw = ToVacuum(mw) - - for rot in rots: - ccftot = [] - for i in range(data.shape[1]): - scf = data[5,i] - scw = data[0,i] - J = np.where(scf!=0)[0] - scw,scf = scw[J],scf[J] - I = np.where((mw>scw[0]-100) & (mw6520)&(mw<6600))[0] + sc[I] = 1. + I = np.where((mw>5888)&(mw<5897))[0] + sc[I] = 1. + I = np.where((mw>4310)&(mw<4360))[0] + sc[I] = 1. + I = np.where((mw>4070)&(mw<4130))[0] + sc[I] = 1. + I = np.where((mw>3875)&(mw<3900))[0] + sc[I] = 1. + I = np.where((mw>3920)&(mw<3945))[0] + sc[I] = 1. + I = np.where((mw>3955)&(mw<3980))[0] + sc[I] = 1. + I = np.where(mw<3850)[0] + sc[I] = 1. + + mw = ToVacuum(mw) + + for rot in rots: + ccftot = [] + for i in range(data.shape[1]): + scf = data[5,i] + scw = data[0,i] + J = np.where(scf!=0)[0] + scw,scf = scw[J],scf[J] + I = np.where((mw>scw[0]-100) & (mw=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]1){ - theArray[i*len_cols+j]=0; - } - } - } - FreeArray(PFinal,len_rows); - free(Range); -/* End of the matrix-to-vector conversion part of the code. Everything's ok up to here...now I have my theArray[i][j] array (matrix) ready to be called...*/ - - -/* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ - - PyObject *lst = PyList_New(len_rows*len_cols); - PyObject *num; - if (!lst) - return NULL; - for (i = 0; i < len_rows*len_cols; i++) { - num=PyFloat_FromDouble(theArray[i]); - if (!num) { - Py_DECREF(lst); - return NULL; - } - PyList_SET_ITEM(lst, i, num); - } - free(theArray); - PyObject *MyResult = Py_BuildValue("O",lst); - Py_DECREF(lst); - return MyResult; + /* + *--------------------------------THE DATA--------------------------------------- + * After initialization of the PyObject pointers, we wish to recover the following inputs: + * + * marray: Vector of the flattened-matrix of the Echelle Spectra data. + * + * varray: Vector containing the trace of the spectrum. + * + * len_rows: Length of the rows of the flattened-matrix. + * + * len_cols: Length of the columns of the flattened-matrix. + * + * len_v: Length of the trace vector. + * + * Length: Given that the trace is centered on the spectrum, Length gives the length, + * in pixels from the center, where we'll consider the spectrum. + * + * RON: The Read Out Noise of our measurements in electrons. + * + * GAIN: The Gain of the detector, in electrons/ADU. + * + * mode: Set this to 0 to apply Marsh's Algorithm (curved trace) to the data. + * Set this to 1 to apply Horne's Algorithm (trace paralell to the rows) to the data. + * + * ------------------------------------------------------------------------------ + */ + PyArg_ParseTuple(args,"OOiiidddddiiii",&marray,&varray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&NSigma,&S,&N,&mode,&col_min,&col_max); + if(col_min!=0){ + col_min=col_min+1; + } + if(col_max!=0){ + col_max=col_max-1; + } + debug=0; + m = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ + v = ARRAYD(varray); + real_cols=len_cols; + real_rows=len_rows; + if(mode!=0) + S=1.0; /* If Horne's algorithm is used, the spacing between + polynomials is 1 */ + Length = CheckAperture(v, len_rows, len_v, S, Length); + K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S + polynomials, rounding the decimal number, from the center. + Then we multiply this number by 2 (to get the total number + of polynomials up and down the center) and add 1 to get the + middle polynomial (the one corresponding to the center). */ + if(debug!=0){ + printf("Number of polynomial to fit: %d \n",K); + } + C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number + is the polynomial number of the center. */ + + double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the detected starting + and final column pixels that "swims in" and "out" of the image, respectively + (FREED here, later). */ + if(debug!=0){ + printf("Range[0]: %f\n",Range[0]); + printf("Range[1]: %f\n",Range[1]); + printf("len_cols=%d, len_rows=%d \n",len_cols,len_rows); + } + range=Range[0]; + + if(Range[0]!=-1){ + if(col_min==0 && col_max==0){ + len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a + cutted image */ + } + else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ + if(Range[0]>=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]1){ + theArray[i*len_cols+j]=0; + } + } + } + FreeArray(PFinal,len_rows); + free(Range); + /* End of the matrix-to-vector conversion part of the code. Everything's ok up to here...now I have my theArray[i][j] array (matrix) ready to be called...*/ + + + /* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ + + PyObject *lst = PyList_New(len_rows*len_cols); + PyObject *num; + if (!lst) + return NULL; + for (i = 0; i < len_rows*len_cols; i++) { + num=PyFloat_FromDouble(theArray[i]); + if (!num) { + Py_DECREF(lst); + return NULL; + } + PyList_SET_ITEM(lst, i, num); + } + free(theArray); + PyObject *MyResult = Py_BuildValue("O",lst); + Py_DECREF(lst); + return MyResult; } /* [INITIALIZATION OF A METHOD] @@ -290,432 +290,425 @@ static PyObject *Marsh_ObtainP(PyObject *self, PyObject *args){ */ static PyObject *Marsh_ObtainSpectrum(PyObject *self, PyObject *args){ - struct timeval tim; - gettimeofday(&tim, NULL); - int i,j,real_cols,real_rows,col_min,col_max; - double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; - double *mflat,*pflat; - double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. - double Length,RON,GAIN,CosmicSigma,S,C; - int len_v,len_cols,len_rows,K,range=-1,debug,isvarimage=0; - PyObject *parray, *marray,*varray; -/* - *--------------------------------THE DATA--------------------------------------- - * After initialization of the PyObject pointers, we wish to recover the following inputs: - * - * marray: Vector of the flattened-matrix of the Echelle Spectra data. - * - * varray: Vector containing the coefficients of the trace of the spectrum. - * - * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. - * - * len_rows: Length of the rows of the flattened-matrix. - * - * len_cols: Length of the columns of the flattened-matrix. - * - * len_v: Length of the coefficient-vector. - * - * Length: Aperture to be used in the obtention of the spectrum. - * - * RON: The Read Out Noise of our measurements. - * - * GAIN: The Gain of the detector, in electrons/ADU. - * - * S: Spacing of the polynomials obtained in the ObtainP function. - * - * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. - * - * ------------------------------------------------------------------------------ -*/ - PyArg_ParseTuple(args,"OOOiiidddddii",&marray,&varray,&parray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); - if(col_min!=0){ - col_min=col_min+1; - } - if(col_max!=0){ - col_max=col_max-1; - } - debug=0; - pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ - mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ - v = ARRAYD(varray); - real_cols=len_cols; - real_rows=len_rows; - Length = CheckAperture(v, len_rows, len_v, S, Length); -/* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ + struct timeval tim; + gettimeofday(&tim, NULL); + int i,j,real_cols,real_rows,col_min,col_max; + double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; + double *mflat,*pflat; + double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. + double Length,RON,GAIN,CosmicSigma,S,C; + int len_v,len_cols,len_rows,K,range=-1,debug,isvarimage=0; + PyObject *parray, *marray,*varray; + /* + *--------------------------------THE DATA--------------------------------------- + * After initialization of the PyObject pointers, we wish to recover the following inputs: + * + * marray: Vector of the flattened-matrix of the Echelle Spectra data. + * + * varray: Vector containing the trace of the spectrum. + * + * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. + * + * len_rows: Length of the rows of the flattened-matrix. + * + * len_cols: Length of the columns of the flattened-matrix. + * + * len_v: Length of the trace vector. + * + * Length: Aperture to be used in the obtention of the spectrum. + * + * RON: The Read Out Noise of our measurements. + * + * GAIN: The Gain of the detector, in electrons/ADU. + * + * S: Spacing of the polynomials obtained in the ObtainP function. + * + * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. + * + * ------------------------------------------------------------------------------ + */ + PyArg_ParseTuple(args,"OOOiiidddddii",&marray,&varray,&parray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); + if(col_min!=0){ + col_min=col_min+1; + } + if(col_max!=0){ + col_max=col_max-1; + } + debug=0; + pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ + mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ + v = ARRAYD(varray); + real_cols=len_cols; + real_rows=len_rows; + Length = CheckAperture(v, len_rows, len_v, S, Length); + /* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ - K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S - polynomials, rounding the decimal number, from the center. - Then we multiply this number by 2 (to get the total number - of polynomials up and down the center) and add 1 to get the - middle polynomial (the one corresponding to the center). */ - C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number - is the polynomial number of the center. */ - range=0; - double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the - detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ - range=Range[0]; - if(debug!=0){ - printf("Range[0]: %f\n",Range[0]); - printf("Range[1]: %f\n",Range[1]); - dvalue=1018.0; - } - - if(Range[0]!=-1){ - if(col_min==0 && col_max==0){ - len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a - cutted image */ - } - else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ - if(Range[0]>=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]Range[1]) - theArray[i*real_cols+j]=0; - else - theArray[i*real_cols+j]=Spectrum[i][j-range]; - } - } - } - - FreeArray(Spectrum,3); - free(Range); - -/* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ - - PyObject *lst = PyList_New(real_cols*3); - if (!lst) - return NULL; - for (i = 0; i < 3*real_cols; i++) { - PyObject *num = PyFloat_FromDouble(theArray[i]); - if (!num) { - Py_DECREF(lst); - return NULL; - } - PyList_SET_ITEM(lst, i, num); - } - free(theArray); - free(pmin); - free(pmax); - PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); - Py_DECREF(lst); - return MyResult; + K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S + polynomials, rounding the decimal number, from the center. + Then we multiply this number by 2 (to get the total number + of polynomials up and down the center) and add 1 to get the + middle polynomial (the one corresponding to the center). */ + C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number + is the polynomial number of the center. */ + range=0; + double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the + detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ + range=Range[0]; + if(debug!=0){ + printf("Range[0]: %f\n",Range[0]); + printf("Range[1]: %f\n",Range[1]); + dvalue=1018.0; + } + + if(Range[0]!=-1){ + if(col_min==0 && col_max==0){ + len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a + cutted image */ + } + else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ + if(Range[0]>=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]Range[1]) + theArray[i*real_cols+j]=0; + else + theArray[i*real_cols+j]=Spectrum[i][j-range]; + } + } + } + + FreeArray(Spectrum,3); + free(Range); + + /* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ + + PyObject *lst = PyList_New(real_cols*3); + if (!lst) + return NULL; + for (i = 0; i < 3*real_cols; i++) { + PyObject *num = PyFloat_FromDouble(theArray[i]); + if (!num) { + Py_DECREF(lst); + return NULL; + } + PyList_SET_ITEM(lst, i, num); + } + free(theArray); + free(pmin); + free(pmax); + PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); + Py_DECREF(lst); + return MyResult; } /* [INITIALIZATION OF A METHOD] -*------------------------THE SOBTAINP METHOD----------------------------- -* PyObject initialization: We define a PyObject which defines a Method -* for the Marsh module: The ObtainP method which returns the P[i][j] spatial -* light fractions back to Python. BE CAREFUL WITH THE INDEXES, remember -* that i=rows, j=columns. According to Marsh's (1989) variables, j=X and -* i=Y. -*---------------------------------------------------------------------- -*/ + *------------------------THE SOBTAINP METHOD----------------------------- + * PyObject initialization: We define a PyObject which defines a Method + * for the Marsh module: The ObtainP method which returns the P[i][j] spatial + * light fractions back to Python. BE CAREFUL WITH THE INDEXES, remember + * that i=rows, j=columns. According to Marsh's (1989) variables, j=X and + * i=Y. + *---------------------------------------------------------------------- + */ static PyObject *Marsh_SObtainP(PyObject *self, PyObject *args){ - struct timeval tim; - gettimeofday(&tim, NULL); - double t1=tim.tv_sec+(tim.tv_usec/1000000.0); - double *m,*varianceimage,*bimage,dvalue=0; - double *v,*pmin,*pmax; // pmin,pmax: FREED (on the MarshIteration function) - double C,S,Length,RON,GAIN,NSigma; - int len_v,K,N,len_cols,len_rows,mode,range=-1,debug,isvarimage=1; - int i,j,real_cols,real_rows,col_min,col_max; - PyObject *marray, *varray,*vararray,*barray; + struct timeval tim; + gettimeofday(&tim, NULL); + double t1=tim.tv_sec+(tim.tv_usec/1000000.0); + double *m,*varianceimage,*bimage,dvalue=0; + double *v,*pmin,*pmax; // pmin,pmax: FREED (on the MarshIteration function) + double C,S,Length,RON,GAIN,NSigma; + int len_v,K,N,len_cols,len_rows,mode,range=-1,debug,isvarimage=1; + int i,j,real_cols,real_rows,col_min,col_max; + PyObject *marray, *varray,*vararray,*barray; -/* - *--------------------------------THE DATA--------------------------------------- - * After initialization of the PyObject pointers, we wish to recover the following inputs: - * - * marray: Vector of the flattened-matrix of the Echelle Spectra data. - * - * varray: Vector containing the coefficients of the trace of the spectrum. - * - * len_rows: Length of the rows of the flattened-matrix. - * - * len_cols: Length of the columns of the flattened-matrix. - * - * len_v: Length of the coefficient-vector. - * - * Length: Given that the trace is centered on the spectrum, Length gives the length, - * in pixels from the center, where we'll consider the spectrum. - * - * RON: The Read Out Noise of our measurements in electrons. - * - * GAIN: The Gain of the detector, in electrons/ADU. - * - * mode: Set this to 0 to apply Marsh's Algorithm (curved trace) to the data. - * Set this to 1 to apply Horne's Algorithm (trace paralell to the rows) to the data. - * - * ------------------------------------------------------------------------------ -*/ - PyArg_ParseTuple(args,"OOOOiiidddddiiii",&marray,&barray,&vararray,&varray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&NSigma,&S,&N,&mode,&col_min,&col_max); - if(col_min!=0){ - col_min=col_min+1; - } - if(col_max!=0){ - col_max=col_max-1; - } - debug=0; - m = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ - v = ARRAYD(varray); - varianceimage = ARRAYD(vararray); - bimage = ARRAYD(barray); - real_cols=len_cols; - real_rows=len_rows; - if(mode!=0) - S=1.0; /* If Horne's algorithm is used, the spacing between - polynomials is 1 */ - Length = CheckAperture(v, len_rows, len_v, S, Length); - K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S - polynomials, rounding the decimal number, from the center. - Then we multiply this number by 2 (to get the total number - of polynomials up and down the center) and add 1 to get the - middle polynomial (the one corresponding to the center). */ - if(debug!=0){ - printf("Number of polynomial to fit: %d \n",K); - dvalue=1018.0; - } - C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number - is the polynomial number of the center. */ - - double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the detected starting - and final column pixels that "swims in" and "out" of the image, respectively - (FREED here, later). */ - if(debug!=0){ - printf("Range[0]: %f\n",Range[0]); - printf("Range[1]: %f\n",Range[1]); - printf("len_cols=%d, len_rows=%d \n",len_cols,len_rows); - } - range=Range[0]; - - if(Range[0]!=-1){ - if(col_min==0 && col_max==0){ - len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a - cutted image */ - } - else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ - if(Range[0]>=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]1){ - theArray[i*len_cols+j]=0; - } - } - } - FreeArray(PFinal,len_rows); - free(Range); -/* End of the matrix-to-vector conversion part of the code. Everything's ok up to here...now I have my theArray[i][j] array (matrix) ready to be called...*/ - - -/* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ - - PyObject *lst = PyList_New(len_rows*len_cols); - PyObject *num; - if (!lst) - return NULL; - for (i = 0; i < len_rows*len_cols; i++) { - num=PyFloat_FromDouble(theArray[i]); - if (!num) { - Py_DECREF(lst); - return NULL; - } - PyList_SET_ITEM(lst, i, num); - } - free(theArray); - PyObject *MyResult = Py_BuildValue("O",lst); - Py_DECREF(lst); - return MyResult; + if(Range[0]==-1){ + for(i=0;i1){ + theArray[i*len_cols+j]=0; + } + } + } + FreeArray(PFinal,len_rows); + free(Range); + /* End of the matrix-to-vector conversion part of the code. Everything's ok up to here...now I have my theArray[i][j] array (matrix) ready to be called...*/ + + + /* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ + + PyObject *lst = PyList_New(len_rows*len_cols); + PyObject *num; + if (!lst) + return NULL; + for (i = 0; i < len_rows*len_cols; i++) { + num=PyFloat_FromDouble(theArray[i]); + if (!num) { + Py_DECREF(lst); + return NULL; + } + PyList_SET_ITEM(lst, i, num); + } + free(theArray); + PyObject *MyResult = Py_BuildValue("O",lst); + Py_DECREF(lst); + return MyResult; } /* [INITIALIZATION OF A METHOD] @@ -723,621 +716,629 @@ static PyObject *Marsh_SObtainP(PyObject *self, PyObject *args){ */ static PyObject *Marsh_SObtainSpectrum(PyObject *self, PyObject *args){ - struct timeval tim; - gettimeofday(&tim, NULL); - int i,j,real_cols,real_rows,col_min,col_max; - double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; - double *mflat,*varflat,*pflat,*bflat; - double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. - double Length,RON,GAIN,CosmicSigma,S,C; - int len_v,len_cols,len_rows,K,range=-1,debug,isvarimage=1; - PyObject *parray, *marray,*varray,*vararray,*barray; -/* - *--------------------------------THE DATA--------------------------------------- - * After initialization of the PyObject pointers, we wish to recover the following inputs: - * - * marray: Vector of the flattened-matrix of the Echelle Spectra data. - * - * varray: Vector containing the coefficients of the trace of the spectrum. - * - * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. - * - * len_rows: Length of the rows of the flattened-matrix. - * - * len_cols: Length of the columns of the flattened-matrix. - * - * len_v: Length of the coefficient-vector. - * - * Length: Aperture to be used in the obtention of the spectrum. - * - * RON: The Read Out Noise of our measurements. - * - * GAIN: The Gain of the detector, in electrons/ADU. - * - * S: Spacing of the polynomials obtained in the ObtainP function. - * - * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. - * - * ------------------------------------------------------------------------------ -*/ - PyArg_ParseTuple(args,"OOOOOiiidddddii",&marray,&barray,&vararray,&varray,&parray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); - if(col_min!=0){ - col_min=col_min+1; - } - if(col_max!=0){ - col_max=col_max-1; - } - debug=0; - pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ - mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ - varflat = ARRAYD(vararray); - bflat = ARRAYD(barray); - v = ARRAYD(varray); - real_cols=len_cols; - real_rows=len_rows; - Length = CheckAperture(v, len_rows, len_v, S, Length); -/* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ + struct timeval tim; + gettimeofday(&tim, NULL); + int i,j,real_cols,real_rows,col_min,col_max; + double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; + double *mflat,*varflat,*pflat,*bflat; + double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. + double Length,RON,GAIN,CosmicSigma,S,C; + int len_v,len_cols,len_rows,K,range=-1,debug,isvarimage=1; + PyObject *parray, *marray,*varray,*vararray,*barray; + /* + *--------------------------------THE DATA--------------------------------------- + * After initialization of the PyObject pointers, we wish to recover the following inputs: + * + * marray: Vector of the flattened-matrix of the Echelle Spectra data. + * barray: Bad pixel map (1=good, other values=bad) + * vararray: Vector of variance guesses + * varray: Vector containing the trace of the spectrum. + * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. + * len_rows: Length of the rows of the flattened-matrix. + * len_cols: Length of the columns of the flattened-matrix. + * len_v: Length of the trace vector + * Length: Aperture to be used in the obtention of the spectrum. + * RON: The Read Out Noise of our measurements. + * GAIN: The Gain of the detector, in electrons/ADU. + * S: Spacing of the polynomials obtained in the ObtainP function. + * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. + * + * ------------------------------------------------------------------------------ + */ + PyArg_ParseTuple(args,"OOOOOiiidddddii",&marray,&barray,&vararray,&varray,&parray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); + if(col_min!=0){ + col_min=col_min+1; + } + if(col_max!=0){ + col_max=col_max-1; + } + debug=0; + pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ + mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ + varflat = ARRAYD(vararray); + bflat = ARRAYD(barray); + v = ARRAYD(varray); + real_cols=len_cols; + real_rows=len_rows; + Length = CheckAperture(v, len_rows, len_v, S, Length); + /* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ - K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S - polynomials, rounding the decimal number, from the center. - Then we multiply this number by 2 (to get the total number - of polynomials up and down the center) and add 1 to get the - middle polynomial (the one corresponding to the center). */ - C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number - is the polynomial number of the center. */ - range=0; - double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the - detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ - range=Range[0]; - if(debug!=0){ - printf("Range[0]: %f\n",Range[0]); - printf("Range[1]: %f\n",Range[1]); - dvalue=1018.0; - } - - if(Range[0]!=-1){ - if(col_min==0 && col_max==0){ - len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a - cutted image */ - } - else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ - if(Range[0]>=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]Range[1]) - theArray[i*real_cols+j]=0; - else - theArray[i*real_cols+j]=Spectrum[i][j-range]; - } - } - } - - FreeArray(Spectrum,3); - free(Range); - -/* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ - - PyObject *lst = PyList_New(real_cols*3); - if (!lst) - return NULL; - for (i = 0; i < 3*real_cols; i++) { - PyObject *num = PyFloat_FromDouble(theArray[i]); - if (!num) { - Py_DECREF(lst); - return NULL; - } - PyList_SET_ITEM(lst, i, num); - } - free(theArray); - free(pmin); - free(pmax); - PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); - Py_DECREF(lst); - return MyResult; + K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S + polynomials, rounding the decimal number, from the center. + Then we multiply this number by 2 (to get the total number + of polynomials up and down the center) and add 1 to get the + middle polynomial (the one corresponding to the center). */ + C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number + is the polynomial number of the center. */ + range=0; + double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the + detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ + range=Range[0]; + if(debug!=0){ + printf("Range[0]: %f\n",Range[0]); + printf("Range[1]: %f\n",Range[1]); + dvalue=1018.0; + } + + if(Range[0]!=-1){ + if(col_min==0 && col_max==0){ + len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a + cutted image */ + } + else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ + if(Range[0]>=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]Range[1]) + theArray[i*real_cols+j]=0; + else + theArray[i*real_cols+j]=Spectrum[i][j-range]; + } + } + } + + FreeArray(Spectrum,3); + free(Range); + + /* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ + + PyObject *lst = PyList_New(real_cols*3); + if (!lst) + return NULL; + for (i = 0; i < 3*real_cols; i++) { + PyObject *num = PyFloat_FromDouble(theArray[i]); + if (!num) { + Py_DECREF(lst); + return NULL; + } + PyList_SET_ITEM(lst, i, num); + } + free(theArray); + free(pmin); + free(pmax); + PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); + Py_DECREF(lst); + return MyResult; } static PyObject *Marsh_BObtainSpectrum(PyObject *self, PyObject *args){ - struct timeval tim; - gettimeofday(&tim, NULL); - int i,j,real_cols,real_rows,col_min,col_max; - double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; - double *mflat,*pflat,*bflat; - double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. - double Length,RON,GAIN,CosmicSigma,S,C; - int len_v,len_cols,len_rows,K,range=-1,debug; - PyObject *parray, *marray,*varray,*barray; -/* - *--------------------------------THE DATA--------------------------------------- - * After initialization of the PyObject pointers, we wish to recover the following inputs: - * - * marray: Vector of the flattened-matrix of the Echelle Spectra data. - * - * varray: Vector containing the coefficients of the trace of the spectrum. - * - * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. - * - * len_rows: Length of the rows of the flattened-matrix. - * - * len_cols: Length of the columns of the flattened-matrix. - * - * len_v: Length of the coefficient-vector. - * - * Length: Aperture to be used in the obtention of the spectrum. - * - * RON: The Read Out Noise of our measurements. - * - * GAIN: The Gain of the detector, in electrons/ADU. - * - * S: Spacing of the polynomials obtained in the ObtainP function. - * - * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. - * - * ------------------------------------------------------------------------------ -*/ - PyArg_ParseTuple(args,"OOOOiiidddddii",&marray,&varray,&parray,&barray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); - if(col_min!=0){ - col_min=col_min+1; - } - if(col_max!=0){ - col_max=col_max-1; - } - debug=0; - pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ - mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ - bflat = ARRAYD(barray); /* We convert our PyObject struct pointers to C-vector array */ - v = ARRAYD(varray); - real_cols=len_cols; - real_rows=len_rows; - Length = CheckAperture(v, len_rows, len_v, S, Length); -/* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ + struct timeval tim; + gettimeofday(&tim, NULL); + int i,j,real_cols,real_rows,col_min,col_max; + double t1=tim.tv_sec+(tim.tv_usec/1000000.0),dvalue=0; + double *mflat,*pflat,*bflat; + double *v,*pmin,*pmax; // (pmin,pmax) freed here, later. + double Length,RON,GAIN,CosmicSigma,S,C; + int len_v,len_cols,len_rows,K,range=-1,debug; + PyObject *parray, *marray,*varray,*barray; + /* + *--------------------------------THE DATA--------------------------------------- + * After initialization of the PyObject pointers, we wish to recover the following inputs: + * + * marray: Vector of the flattened-matrix of the Echelle Spectra data. + * + * varray: Vector containing the trace of the spectrum. + * + * parray: Vector of the flattened-matrix of the Echelle Spectra P_{ij} coefficents. + * + * len_rows: Length of the rows of the flattened-matrix. + * + * len_cols: Length of the columns of the flattened-matrix. + * + * len_v: Length of the trace vector. + * + * Length: Aperture to be used in the obtention of the spectrum. + * + * RON: The Read Out Noise of our measurements. + * + * GAIN: The Gain of the detector, in electrons/ADU. + * + * S: Spacing of the polynomials obtained in the ObtainP function. + * + * CosmicSigma: Number of times sigma is multiplied by to reject Cosmic Rays. + * + * ------------------------------------------------------------------------------ + */ + PyArg_ParseTuple(args,"OOOOiiidddddii",&marray,&varray,&parray,&barray,&len_rows,&len_cols,&len_v,&Length,&RON,&GAIN,&S,&CosmicSigma,&col_min,&col_max); + if(col_min!=0){ + col_min=col_min+1; + } + if(col_max!=0){ + col_max=col_max-1; + } + debug=0; + pflat = ARRAYD(parray); /* We convert our PyObject struct pointers to C-vector array */ + mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ + bflat = ARRAYD(barray); /* We convert our PyObject struct pointers to C-vector array */ + v = ARRAYD(varray); + real_cols=len_cols; + real_rows=len_rows; + Length = CheckAperture(v, len_rows, len_v, S, Length); + /* Start of the algorithm for the spectrum obtention (faster with the transpose!): */ - K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S - polynomials, rounding the decimal number, from the center. - Then we multiply this number by 2 (to get the total number - of polynomials up and down the center) and add 1 to get the - middle polynomial (the one corresponding to the center). */ - C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number - is the polynomial number of the center. */ - range=0; - double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the - detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ - range=Range[0]; - if(debug!=0){ - printf("Range[0]: %f\n",Range[0]); - printf("Range[1]: %f\n",Range[1]); - dvalue=0.0; - } - - if(Range[0]!=-1){ - if(col_min==0 && col_max==0){ - len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a - cutted image */ - } - else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ - if(Range[0]>=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]Range[1]) - theArray[i*real_cols+j]=0; - else - theArray[i*real_cols+j]=Spectrum[i][j-range]; - } - } - } - - FreeArray(Spectrum,4); - free(Range); - -/* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ - - PyObject *lst = PyList_New(real_cols*4); - if (!lst) - return NULL; - for (i = 0; i < 4*real_cols; i++) { - PyObject *num = PyFloat_FromDouble(theArray[i]); - if (!num) { - Py_DECREF(lst); - return NULL; - } - PyList_SET_ITEM(lst, i, num); - } - free(theArray); - free(pmin); - free(pmax); - PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); - Py_DECREF(lst); - return MyResult; + K=(int)(2*(int)((Length/S)+0.5)+1); /* The number of polynomials. The idea is to form n=Length/S + polynomials, rounding the decimal number, from the center. + Then we multiply this number by 2 (to get the total number + of polynomials up and down the center) and add 1 to get the + middle polynomial (the one corresponding to the center). */ + C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number + is the polynomial number of the center. */ + range=0; + double* Range = RangeDetector(v,C,S,len_v,K,len_rows,len_cols,range); /* This vector containts the + detected starting and final column pixels that "swims in" and "out" of the image, respectively (FREED here) */ + range=Range[0]; + if(debug!=0){ + printf("Range[0]: %f\n",Range[0]); + printf("Range[1]: %f\n",Range[1]); + dvalue=0.0; + } + + if(Range[0]!=-1){ + if(col_min==0 && col_max==0){ + len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a + cutted image */ + } + else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ + if(Range[0]>=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]Range[1]) + theArray[i*real_cols+j]=0; + else + theArray[i*real_cols+j]=Spectrum[i][j-range]; + } + } + } + + FreeArray(Spectrum,4); + free(Range); + + /* Finally, we create a Python "Object" List that contains the P coefficients and return it back to Python */ + + PyObject *lst = PyList_New(real_cols*4); + if (!lst) + return NULL; + for (i = 0; i < 4*real_cols; i++) { + PyObject *num = PyFloat_FromDouble(theArray[i]); + if (!num) { + Py_DECREF(lst); + return NULL; + } + PyList_SET_ITEM(lst, i, num); + } + free(theArray); + free(pmin); + free(pmax); + PyObject *MyResult = Py_BuildValue("Oi",lst,real_cols); + Py_DECREF(lst); + return MyResult; } -static PyObject *Marsh_SimpleExtraction(PyObject *self, PyObject *args){ - struct timeval tim; - gettimeofday(&tim, NULL); - int i,j,real_cols,debug=0,col_min,col_max; - double t1=tim.tv_sec+(tim.tv_usec/1000000.0),vMin,vMax,dvalue=0; - double *mflat; - double *v; - double Length,C,S; - int len_v,len_cols,len_rows,K,range; - PyObject *marray,*varray; -/* - *--------------------------------THE DATA--------------------------------------- - * After initialization of the PyObject pointers, we wish to recover the following inputs: - * - * marray: Vector of the flattened-matrix of the Echelle Spectra data. - * - * varray: Vector containing the coefficients of the trace of the spectrum. - * - * len_rows: Length of the rows of the flattened-matrix. - * - * len_cols: Length of the columns of the flattened-matrix. - * - * len_v: Length of the coefficient-vector. - * - * Length: Aperture (in pixels) to be taken from the center to cover the entire spectrum. - * - * ------------------------------------------------------------------------------ -*/ - PyArg_ParseTuple(args,"OOiiidii",&marray,&varray,&len_rows,&len_cols,&len_v,&Length,&col_min,&col_max); - if(col_min!=0){ - col_min=col_min+1; - } - if(col_max!=0){ - col_max=col_max-1; - } - range=0; - mflat = ARRAYD(marray); /* We convert our PyObject struct pointers to C-vector array */ - v = ARRAYD(varray); - real_cols=len_cols; - S=1.0; - Length = CheckAperture(v, len_rows, len_v, S, Length); - K=(int)(2*(int)((Length/S))+1); - - C=-(S*(1.0+(((double)K-1.0)/2.0))); /* We get the C constant for the Q coefficients. This number - is the polynomial number of the center. */ - - double* Range = SimpleRangeDetector(v,Length,len_v,len_rows,len_cols,range); /* This vector - containts the detected starting and final column pixels that "swims in" and "out" of the image, respectively (F) */ - range=Range[0]; - - if(debug!=0){ - printf("Range[0]: %f\n",Range[0]); - printf("Range[1]: %f\n",Range[1]); - dvalue=1018.0; - } - - if(Range[0]!=-1){ - if(col_min==0 && col_max==0){ - len_cols=Range[1]-Range[0]+1; /* If the column presents problems, we start by creating a - cutted image */ - } - else if((col_min!=0 && col_max!=0) || (col_min==0 && col_max!=0)){ - if(Range[0]>=col_min && Range[1]<=col_max){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]col_max){ - range=col_min; - Range[0]=col_min; - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]>=col_min && Range[1]>col_max){ - Range[1]=col_max; - len_cols=Range[1]-Range[0]+1; - } - } - else if(col_min!=0 && col_max==0){ - if(Range[0]>=col_min){ - len_cols=Range[1]-Range[0]+1; - } - else if(Range[0]=col_min && Range[1]<=col_max){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]col_max){ + range=col_min; + Range[0]=col_min; + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]>=col_min && Range[1]>col_max){ + Range[1]=col_max; + len_cols=Range[1]-Range[0]+1; + } + } + else if(col_min!=0 && col_max==0){ + if(Range[0]>=col_min){ + len_cols=Range[1]-Range[0]+1; + } + else if(Range[0]=range && i=range && i= 3 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "Marsh", + NULL, + 0, //no state, so re-initialization is fine + MarshMethods, + NULL, + NULL, + NULL, + NULL }; +PyMODINIT_FUNC PyInit_Marsh(void) +{ + PyObject* module = PyModule_Create(&moduledef); + return module; +} + +#else void initMarsh(void){ - (void) Py_InitModule("Marsh", MarshMethods); + Py_InitModule("Marsh", MarshMethods); } +#endif /********************************************************************* @@ -1346,54 +1347,54 @@ void initMarsh(void){ */ double** Transpose(double **O,int rowsIN,int colsIN){ -int i,j; -double **OT=MakeArray(colsIN,rowsIN); -for(i=0;i= 0.5+S) - return 0.0; - else if(d+S <= 0.5) - return S; - else if(d <= 0.5){ - return ((S/2.0)+(0.5-d)-(pow(0.5-d,2.0)/(2.0*S))); - } - else if(d > 0.5){ - return ((0.5-d)+(pow(0.5-d,2.0)/(2.0*S))+S/2); - } - else - return -999; + double d,x=Trace(j,v,len_v,range)+(S*(double)k)+C; // We compute the position of the center of the polynomial. + d=fabs(x-(double)i); + if(d>= 0.5+S) + return 0.0; + else if(d+S <= 0.5) + return S; + else if(d <= 0.5){ + return ((S/2.0)+(0.5-d)-(pow(0.5-d,2.0)/(2.0*S))); + } + else if(d > 0.5){ + return ((0.5-d)+(pow(0.5-d,2.0)/(2.0*S))+S/2); + } + else + return -999; } double** getQ(double *v,double C,double S,int len_v,int K,int len_cols,int range,double* pmin,double* pmax,int mode){ - int i,j,vMin,vMax,contador,k; - contador=0; - for(j=0;j=SigmaSquared){ - A[j][i]=-9999; - counter++; - } - } + int i,j,vMin,vMax,counter=0; + double TotalSum=0,SigmaSquared,Ratio; + SigmaSquared=pow(NSigma,2.0); + for(j=0;j=SigmaSquared){ + A[j][i]=-9999; + counter++; + } + } } - } - if(debug!=0){ - printf("Number of outliers found: %d \n",counter); - printf("Chi-squared of the fit: %f \n",TotalSum); - } - if(counter==0){ - return 0; - } - return 1; + } + if(debug!=0){ + printf("Number of outliers found: %d \n",counter); + printf("Chi-squared of the fit: %f \n",TotalSum); + } + if(counter==0){ + return 0; + } + return 1; } /* [FUNCTION] * ------------------------ Marsh's algorithm---------------------------------- @@ -1809,552 +1810,551 @@ int OutlierRejection(double** A,double** V,double** P,double** E,double** VarE,d */ double** MarshIteration(double** M,double** VarImage,double* pmin,double* pmax,double *v,double C,double S,double RON,double GAIN,int len_v,int len_rows,int len_cols,int N,int K,int mode,int range,int debug,double NSigma, int isvarimage){ -int Iteration=1,IterNum=0; -double** P=MakeArray(len_cols,len_rows); // FREED on Transpose. -double** V=MakeArray(len_cols,len_rows); // FREED here, later. -double** E=MakeArray(len_cols,len_rows); // FREED here, later. -double** VarE=MakeArray(len_cols,len_rows); // FREED here, later. -double** C_qp=MakeArray(N*K,N*K); /* Matrix that contains the coefficients for the linear system (FREED here, later) */ -double** J=MakeArray(2*(N-1)+1,len_cols); // FREED here, later. - - // All from here to the end, FREED, later. - -double* B=MakeVector(N*K); /* Vector with the solutions to the linear system. */ -double* X=MakeVector(N*K); /* Vector with the coefficients for the linear system. Given - these elements, we wish to solve the C_qp B_q=X_q system */ -double* RS=MakeVector(len_cols); /* Vector that saves the sum of pixel values of the rows - (columns) of the transposed (untransposed) M matrix. */ -double* VarRS=MakeVector(len_cols); /* Vector that saves the sum of pixel values variances of the rows - (columns) of the transposed (untransposed) M matrix */ -PixelResampling(M,pmin,pmax,len_cols); -double** Q=getQ(v,C,S,len_v,K,len_cols,range,pmin,pmax,mode); /* First we obtain the Q matrix (note: independant of fit) */ -getJ(J,N,len_cols); -getImageVariances(M,V,VarImage,pmin,pmax,RON,GAIN,len_cols,isvarimage); -getRowSum(M,RS,v,pmin,pmax,len_cols,len_v); -getE(M,RS,E,pmin,pmax,v,len_rows,len_cols); -getVarRowSum(M,V,VarRS,pmin,pmax,RON,GAIN,len_cols); -getVarE(M,E,RS,VarRS,VarE,V,pmin,pmax,RON,GAIN,len_rows,len_cols); - while(Iteration != 0){ /* If Iteration=1, we continue iterating! */ - IterNum+=1; - if(debug!=0){ - printf("------------------- \n"); - printf("Iteration number %d \n",IterNum); - printf("------------------- \n"); - printf("Obtaining the profiles...\n"); /* We obtain Var(E_ij), the variances of E_ij on each pixel */ - } - getC(M,Q,VarE,C_qp,J,pmin,pmax,K,N,len_cols); /* We obtain C_qp, the matrix with the C coefficients for - the fit */ - getX(M,Q,E,VarE,X,J,pmin,pmax,K,N,len_cols); /* We obtain X_q, the vector with the coefficients for the fit */ - LinearSolver(C_qp,X,B,N*K); /* Solve the linear system C_qp*B_q=X_q, obtaining the - B_q vector */ - getP(B,Q,P,J,pmin,pmax,K,N,len_rows,len_cols,mode); /* We obtain P, the model light fractions on each pixel */ - Renormalize(P,pmin,pmax,len_rows,len_cols); - VarRevision(M,VarImage,V,RS,P,pmin,pmax,len_cols,RON,GAIN,isvarimage); - getVarRowSum(M,V,VarRS,pmin,pmax,RON,GAIN,len_cols); - getVarE(M,E,RS,VarRS,VarE,V,pmin,pmax,RON,GAIN,len_rows,len_cols); - if(IterNum!=1) - Iteration=OutlierRejection(M,V,P,E,VarE,RS,pmin,pmax,NSigma,len_cols,debug); - } -FreeArray(E,len_cols); /* Free our vectors and matrices */ -FreeArray(VarE,len_cols); -FreeArray(C_qp,N*K); -FreeArray(Q,K); -FreeArray(J,2*(N-1)+1); -FreeArray(M,len_cols); -FreeArray(VarImage,len_cols); -FreeArray(V,len_cols); -free(B); -free(X); -free(RS); -free(VarRS); -free(pmin); -free(pmax); -if(debug!=0){ - printf("Going out of MarshIteration...\n"); -} -return Transpose(P,len_cols,len_rows); + int Iteration=1,IterNum=0; + double** P=MakeArray(len_cols,len_rows); // FREED on Transpose. + double** V=MakeArray(len_cols,len_rows); // FREED here, later. + double** E=MakeArray(len_cols,len_rows); // FREED here, later. + double** VarE=MakeArray(len_cols,len_rows); // FREED here, later. + double** C_qp=MakeArray(N*K,N*K); /* Matrix that contains the coefficients for the linear system (FREED here, later) */ + double** J=MakeArray(2*(N-1)+1,len_cols); // FREED here, later. + + // All from here to the end, FREED, later. + + double* B=MakeVector(N*K); /* Vector with the solutions to the linear system. */ + double* X=MakeVector(N*K); /* Vector with the coefficients for the linear system. Given + these elements, we wish to solve the C_qp B_q=X_q system */ + double* RS=MakeVector(len_cols); /* Vector that saves the sum of pixel values of the rows + (columns) of the transposed (untransposed) M matrix. */ + double* VarRS=MakeVector(len_cols); /* Vector that saves the sum of pixel values variances of the rows + (columns) of the transposed (untransposed) M matrix */ + PixelResampling(M,pmin,pmax,len_cols); + double** Q=getQ(v,C,S,len_v,K,len_cols,range,pmin,pmax,mode); /* First we obtain the Q matrix (note: independant of fit) */ + getJ(J,N,len_cols); + getImageVariances(M,V,VarImage,pmin,pmax,RON,GAIN,len_cols,isvarimage); + getRowSum(M,RS,v,pmin,pmax,len_cols,len_v); + getE(M,RS,E,pmin,pmax,v,len_rows,len_cols); + getVarRowSum(M,V,VarRS,pmin,pmax,RON,GAIN,len_cols); + getVarE(M,E,RS,VarRS,VarE,V,pmin,pmax,RON,GAIN,len_rows,len_cols); + while(Iteration != 0){ /* If Iteration=1, we continue iterating! */ + IterNum+=1; + if(debug!=0){ + printf("------------------- \n"); + printf("Iteration number %d \n",IterNum); + printf("------------------- \n"); + printf("Obtaining the profiles...\n"); /* We obtain Var(E_ij), the variances of E_ij on each pixel */ + } + getC(M,Q,VarE,C_qp,J,pmin,pmax,K,N,len_cols); /* We obtain C_qp, the matrix with the C coefficients for + the fit */ + getX(M,Q,E,VarE,X,J,pmin,pmax,K,N,len_cols); /* We obtain X_q, the vector with the coefficients for the fit */ + LinearSolver(C_qp,X,B,N*K); /* Solve the linear system C_qp*B_q=X_q, obtaining the + B_q vector */ + getP(B,Q,P,J,pmin,pmax,K,N,len_rows,len_cols,mode); /* We obtain P, the model light fractions on each pixel */ + Renormalize(P,pmin,pmax,len_rows,len_cols); + VarRevision(M,VarImage,V,RS,P,pmin,pmax,len_cols,RON,GAIN,isvarimage); + getVarRowSum(M,V,VarRS,pmin,pmax,RON,GAIN,len_cols); + getVarE(M,E,RS,VarRS,VarE,V,pmin,pmax,RON,GAIN,len_rows,len_cols); + if(IterNum!=1) + Iteration=OutlierRejection(M,V,P,E,VarE,RS,pmin,pmax,NSigma,len_cols,debug); + } + FreeArray(E,len_cols); /* Free our vectors and matrices */ + FreeArray(VarE,len_cols); + FreeArray(C_qp,N*K); + FreeArray(Q,K); + FreeArray(J,2*(N-1)+1); + FreeArray(M,len_cols); + FreeArray(VarImage,len_cols); + FreeArray(V,len_cols); + free(B); + free(X); + free(RS); + free(VarRS); + free(pmin); + free(pmax); + if(debug!=0){ + printf("Going out of MarshIteration...\n"); + } + return Transpose(P,len_cols,len_rows); } void Renormalize(double** P,double* pmin,double* pmax,int len_rows,int len_cols){ - int vMin,vMax,j,i; - double PartialSum; - for(j=0;j=fluxcount){ - Ratio=(count-CosmicSigma*countsigma)-(fluxcount+CosmicSigma*fluxcountsigma); - if(Ratio>0){ - detection=1; - } - } - else{ - Ratio=(fluxcount-CosmicSigma*fluxcountsigma)-(count+CosmicSigma*countsigma); - if(Ratio>0){ - detection=1; - } - } - if(detection==1){ - if(DummyRatio=fluxcount){ + Ratio=(count-CosmicSigma*countsigma)-(fluxcount+CosmicSigma*fluxcountsigma); + if(Ratio>0){ + detection=1; + } + } + else{ + Ratio=(fluxcount-CosmicSigma*fluxcountsigma)-(count+CosmicSigma*countsigma); + if(Ratio>0){ + detection=1; + } + } + if(detection==1){ + if(DummyRatio=fluxcount){ - Ratio=(count-CosmicSigma*countsigma)-(fluxcount+CosmicSigma*fluxcountsigma); - if(Ratio>0){ - detection=1; - } - } - else{ - Ratio=(fluxcount-CosmicSigma*fluxcountsigma)-(count+CosmicSigma*countsigma); - if(Ratio>0){ - detection=1; - } - } - if(detection==1){ - if(DummyRatio=fluxcount){ + Ratio=(count-CosmicSigma*countsigma)-(fluxcount+CosmicSigma*fluxcountsigma); + if(Ratio>0){ + detection=1; + } + } + else{ + Ratio=(fluxcount-CosmicSigma*fluxcountsigma)-(count+CosmicSigma*countsigma); + if(Ratio>0){ + detection=1; + } + } + if(detection==1){ + if(DummyRatio=len_rows){ - CentralAperture = CentralAperture - 1; - K = (int)((int)((CentralAperture/S)+0.5)); - lowrow = (int)(center[i]-S*(K+1.0)-0.5); - i = i-1; - } - if(lowrow<0 && uprow>=len_rows){ - CentralAperture = CentralAperture - 1; - } - if(lowrow<0 && uprow=len_rows){ + CentralAperture = CentralAperture - 1; + K = (int)((int)((CentralAperture/S)+0.5)); + lowrow = (int)(center[i]-S*(K+1.0)-0.5); + i = i-1; + } + if(lowrow<0 && uprow>=len_rows){ + CentralAperture = CentralAperture - 1; + } + if(lowrow<0 && uprowTrace(len_cols/2,v,len_v,range) && ((int)(((Trace(0,v,len_v,range)+C)+(double)(K)*S)+0.5)>=len_rows || (int)(((Trace(len_cols-1,v,len_v,range)+C)+(double)(K)*S)+0.5)>=len_rows)){ // The problem is on an upper trace for(j=0;j0 ){ - ReturningVector[1]=j-1; - break; - } + uplim=((Trace(j,v,len_v,range)+C)+(double)(K)*S)+0.5; + if( uplim0 ){ + ReturningVector[1]=j-1; + break; + } } } else if(Trace(0,v,len_v,range)0 ){ - ReturningVector[1]=j-1; - break; - } + downlim=((Trace(j,v,len_v,range)+C)+S)+0.5; + if( downlim==0 && s==0 ){ + s++; + ReturningVector[0]=j; + } + else if( downlim<0 && s>0 ){ + ReturningVector[1]=j-1; + break; + } } } else{ ReturningVector[0]=-1; } -return ReturningVector; + return ReturningVector; } double* SimpleRangeDetector(double *v,double Length,int len_v,int len_rows,int len_cols,int range){ -int j,s=0,uplim=0,downlim=0; -double* ReturningVector = MakeVector(2); -ReturningVector[1]=len_cols; + int j,s=0,uplim=0,downlim=0; + double* ReturningVector = MakeVector(2); + ReturningVector[1]=len_cols; if(Trace(0,v,len_v,range)>Trace(len_cols/2,v,len_v,range) && ((int)((Trace(0,v,len_v,range)+Length)+0.5)>=len_rows || (int)((Trace(len_cols-1,v,len_v,range)+Length)+0.5)>=len_rows)){ // The problem is on an upper trace for(j=0;j0 ){ - ReturningVector[1]=j-1; - break; - } + uplim=(Trace(j,v,len_v,range)+Length)+0.5; + if( uplim0 ){ + ReturningVector[1]=j-1; + break; + } } } else if(Trace(0,v,len_v,range)0 ){ - ReturningVector[1]=j-1; - break; - } + downlim=(Trace(j,v,len_v,range)-Length)+0.5; + if( downlim==0 && s==0 ){ + s++; + ReturningVector[0]=j; + } + else if( downlim<0 && s>0 ){ + ReturningVector[1]=j-1; + break; + } } } else{ ReturningVector[0]=-1; } -return ReturningVector; + return ReturningVector; } diff --git a/utils/OptExtract/README b/utils/OptExtract/README index c32d603..7b08e67 100644 --- a/utils/OptExtract/README +++ b/utils/OptExtract/README @@ -15,18 +15,9 @@ April 13th, 2012 ---> Added special functions SObtainP and SObtainSpectrum. Thes ---------------------------------------------------------------------------------------------------- ~ HOW TO INSTALL THE MODULE ~ ---------------------------------------------------------------------------------------------------- - 1.- On console, as root, do "python setup.py build". + 1.- On console, do "python setup.py install" for a system install. Append "--user" for local install. - 2.- A new folder is created called "build". Inside, you'll find another folder called - lib.OS-MOD-PyVer, where OS is your operating system, MOD is the version and PyVer - is your python version. In my case, it looks like this: - - /build/lib.linux-i686-2.6 - - Inside that folder, a module called "Marsh.so" is created. Move that to the directory - where you are going to perform the extraction (i.e. where your Python code is). - - 3.- That's it! Information on how to use it is given on the Test.py file attached to the package. + 2.- That's it! Information on how to use it is given on the Test.py file attached to the package. ---------------------------------------------------------------------------------------------------- ~ Frequented Asked Questions (FAQ) ~ diff --git a/utils/OptExtract/Test.py b/utils/OptExtract/Test.py index 8ca3256..12e67a6 100644 --- a/utils/OptExtract/Test.py +++ b/utils/OptExtract/Test.py @@ -1,3 +1,4 @@ +from __future__ import print_function # -*- coding: utf-8 -*- import pyfits from pylab import * @@ -15,56 +16,56 @@ Here we pass the matrix, coefficients and lengths to the C programmed module. To call this function of the module we have to do: - + Marsh.ObtainP(M,T,MRows,MCols,TLength,Aperture,RON,GAIN,NSigma,S,N,MODE) - + Where the inputs are: - + M: Matrix of the image containing the pixel values with the spectrum's wavelength direction on the column direction. - It is assumed that the image passed is sky-substracted. - + It is assumed that the image passed is sky-substracted. + T: Trace vector's coefficients (D_N,D_(N-1),D_(N-2)...,D_0) where the trace vector is in the form sum(D_n*x^n), n=0...N. The trace vector is assumed to be on matrix coordinates, with the horizontal 0 (i.e. x/column 0) on the first horizontal pixel (from left to right) to have spectrum data. - + MRows: Number of rows of the M matrix. - + MCols: Number of columns of the M matrix. - + TLength: Number of elements (length) of the trace vector that contains the coefficients. - + Aperture:Number of pixels, from the center pixel, of the aperture radius. - + RON: Read Out Noise of the measurements (in electrons). - + GAIN: Gain of the CCD in electrons/ADU - + NSigma: Number of times we multiply sigma to get a confidence interval. - + S: The spacing between the polynomials to be fitted (see Marsh, 1989). Set this to one if Horne's algorithm is going to be applied. - + N: The number of coefficients of the polynomials to be fitted (see Marsh, 1989, Horne, 1986). - + MODE: Set this to 0 if Marsh's algorithm is desired (curved trace). Any other number will produce Horne's algorithm to be applied to the data. - + min_col: Pixel coordinates of the input data to start operating from. If zero, the MRows input is used instead. - + max_col: Pixel coordinates of the input data to end operating from. If zero, the MCols input is used instead. -The output is a flattened python list that we convert to a Numpy Array (with the asarray() function) and then +The output is a flattened python list that we convert to a Numpy Array (with the asarray() function) and then convert in matrix form doing a resize. """ def PCoeff(filename,b,Aperture,RON,Gain,NSigma,S,N,marsh_alg,min_col,max_col): - hdulist = pyfits.open(filename) # Here we obtain the image... - data=hdulist[0].data # ... and we obtain the image matrix. - Result=Marsh.ObtainP((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),data.shape[0],data.shape[1],data.shape[1],Aperture,RON,Gain,NSigma,S,N,marsh_alg,min_col,max_col) - FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. - FinalMatrix.resize(data.shape[0],data.shape[1]) # And return the array in matrix-form. - return FinalMatrix + hdulist = pyfits.open(filename) # Here we obtain the image... + data=hdulist[0].data # ... and we obtain the image matrix. + Result=Marsh.ObtainP((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),data.shape[0],data.shape[1],data.shape[1],Aperture,RON,Gain,NSigma,S,N,marsh_alg,min_col,max_col) + FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix.resize(data.shape[0],data.shape[1]) # And return the array in matrix-form. + return FinalMatrix """ ---------------------------------------------------------------- @@ -72,58 +73,58 @@ def PCoeff(filename,b,Aperture,RON,Gain,NSigma,S,N,marsh_alg,min_col,max_col): Returns a matrix with three rows: - First row: Pixel coordinates (taken from the first horizontal/column/x point with data) of the fluxes and variances. - Second row: Flux values for the coordinates on the first row, same column. - Third row: Variances of the respective flux values. + First row: Pixel coordinates (taken from the first horizontal/column/x point with data) of the fluxes and variances. + Second row: Flux values for the coordinates on the first row, same column. + Third row: Variances of the respective flux values. ---------------------------------------------------------------- Here we pass the image matrix, the P Coefficients, coefficients of trace vector and lengths to C. To call this function we do: - + Marsh.ObtainSpectrum(M,T,P,MRows,MCols,TLength,Length,RON,GAIN,S,CSigma) - + Where the inputs of the ObtainSpectrum function are: - + M: Matrix of the image containing the pixel values with the spectrum's wavelength direction on the column direction. - + T: Trace vector's coefficients (D_N,D_(N-1),D_(N-2)...,D_0) where the trace vector is in the form sum(D_n*x^n), n=0...N. The trace vector is assumed to be on matrix coordinates, with the horizontal 0 (i.e. x/column 0) on the first horizontal pixel (from left to right) to have spectrum data. - P: Coefficients of each image value obtained on the ObtainP function. - + P: Coefficients of each image value obtained on the ObtainP function. + MRows: Number of rows of the M/P matrix. - + MCols: Number of columns of the M/P matrix. - + TLength: Number of elements (length) of the trace vector that contains the coefficients. - + Length: Number of pixels, from the center pixel, of the aperture radius. - + RON: Read Out Noise of the measurements (in electrons). - + GAIN: Gain of the CCD in electrons/ADU - + S: The spacing between the fitted polynomials. Set this to one if Horne's algorithm was applied. - + CSIgma: Number, in sigmas, for the cosmic ray rejection. - + min_col: Pixel coordinates of the input data to start operating from. If zero, the MRows input is used instead. - + max_col: Pixel coordinates of the input data to end operating from. If zero, the MCols input is used instead. -The output is a flattened python list that we convert to a Numpy Array (with the asarray() function) and then +The output is a flattened python list that we convert to a Numpy Array (with the asarray() function) and then convert in matrix form doing a resize. """ - + def getSpectrum(P,filename,b,Aperture,RON,Gain,S,NCosmic,min_col,max_col): - - hdulist = pyfits.open(filename) # Here we obtain the image... - data=hdulist[0].data # ... and we obtain the image matrix. - Result,size =Marsh.ObtainSpectrum((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),P.flatten(),data.shape[0],data.shape[1],data.shape[1],Aperture,RON,Gain,S,NCosmic,min_col,max_col) - FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. - FinalMatrix.resize(3,size) # And return the array in matrix-form. - return FinalMatrix + + hdulist = pyfits.open(filename) # Here we obtain the image... + data=hdulist[0].data # ... and we obtain the image matrix. + Result,size =Marsh.ObtainSpectrum((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),P.flatten(),data.shape[0],data.shape[1],data.shape[1],Aperture,RON,Gain,S,NCosmic,min_col,max_col) + FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. + FinalMatrix.resize(3,size) # And return the array in matrix-form. + return FinalMatrix # Main function: @@ -136,11 +137,10 @@ def getSpectrum(P,filename,b,Aperture,RON,Gain,S,NCosmic,min_col,max_col): S=0.4 # Spacing between the polynomials. N=3 # Number of coefficients in our polynomials. P=PCoeff(FileName,b,Aperture,RON,Gain,NSigma,S,N,0,0,0) # We get our P-coefficient matrix...that easy! - # (see the PCoeff function for more info). - # In this example we take our spectrum from x=50 to + # (see the PCoeff function for more info). + # In this example we take our spectrum from x=50 to # 1500, where x is in our matrix coordinates. NCosmic=5.0 # Sigma-times to reject cosmic rays. Spectrum=getSpectrum(P,FileName,b,Aperture,RON,Gain,S,NCosmic,0,0) # We obtain the spectrum and variances! plot(Spectrum[0],Spectrum[1],'-') # Plot the spectrum to check if everything went ok. show() - diff --git a/utils/OptExtract/TestSimple.py b/utils/OptExtract/TestSimple.py index b53e8dd..0e64b37 100644 --- a/utils/OptExtract/TestSimple.py +++ b/utils/OptExtract/TestSimple.py @@ -1,3 +1,4 @@ +from __future__ import print_function # -*- coding: utf-8 -*- import pyfits from pylab import * @@ -7,11 +8,11 @@ def getSpectrum(filename,b,Aperture,minimum_column,maximum_column): - hdulist = pyfits.open(filename) # Here we obtain the image... - data=hdulist[0].data # ... and we obtain the image matrix. - Result=Marsh.SimpleExtraction((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),data.shape[0],data.shape[1],data.shape[1],Aperture,minimum_column,maximum_column) - FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. - return FinalMatrix + hdulist = pyfits.open(filename) # Here we obtain the image... + data=hdulist[0].data # ... and we obtain the image matrix. + Result=Marsh.SimpleExtraction((data.flatten()).astype(double),scipy.polyval(b,numpy.arange(data.shape[1])).astype(double),data.shape[0],data.shape[1],data.shape[1],Aperture,minimum_column,maximum_column) + FinalMatrix=asarray(Result) # After the function, we convert our list to a Numpy array. + return FinalMatrix # Main function: diff --git a/utils/OptExtract/setup.py b/utils/OptExtract/setup.py index c202bbc..3320df3 100644 --- a/utils/OptExtract/setup.py +++ b/utils/OptExtract/setup.py @@ -1,9 +1,10 @@ +from __future__ import print_function from distutils.core import setup, Extension import numpy """ According to GSL documentation (http://www.gnu.org/software/gsl/manual/html_node/Shared-Libraries.html), in order to run the different operations one must include the GSL library, the GSLCBLAS library and the math library. To compile in C one must do: - + gcc -Wall -c filename.c And then: @@ -17,5 +18,5 @@ Is as simple as putting libraries=['name1','name2',...] inside the Extension module. Here we do it with "gsl", "gslcblas" and "m". """ -module = Extension('Marsh', sources = ['Marsh.c'],libraries=['gsl','gslcblas','m'], include_dirs=[numpy.get_include(),'/usr/local/include']) +module = Extension('Marsh', sources = ['Marsh.c'],libraries=['gsl','gslcblas','m'], include_dirs=[numpy.get_include(),'/usr/local/include']) setup(name = 'Marsh Algorithm: C/Python Extension ', version = '1.0', ext_modules = [module]) diff --git a/utils/SSEphem/jplephem.py b/utils/SSEphem/jplephem.py index 57b3da9..2f84ff5 100644 --- a/utils/SSEphem/jplephem.py +++ b/utils/SSEphem/jplephem.py @@ -1,3 +1,4 @@ +from __future__ import print_function # This file was automatically generated by SWIG (http://www.swig.org). # Version 2.0.12 # @@ -71,76 +72,74 @@ class _object : pass def set_ephemeris_dir(*args): - return _jplephem.set_ephemeris_dir(*args) + return _jplephem.set_ephemeris_dir(*args) set_ephemeris_dir = _jplephem.set_ephemeris_dir def object_track(*args): - return _jplephem.object_track(*args) + return _jplephem.object_track(*args) object_track = _jplephem.object_track def object_doppler(*args): - return _jplephem.object_doppler(*args) + return _jplephem.object_doppler(*args) object_doppler = _jplephem.object_doppler def doppler_fraction(*args): - return _jplephem.doppler_fraction(*args) + return _jplephem.doppler_fraction(*args) doppler_fraction = _jplephem.doppler_fraction def observer_position_velocity(*args): - return _jplephem.observer_position_velocity(*args) + return _jplephem.observer_position_velocity(*args) observer_position_velocity = _jplephem.observer_position_velocity def pulse_delay(*args): - return _jplephem.pulse_delay(*args) + return _jplephem.pulse_delay(*args) pulse_delay = _jplephem.pulse_delay def utc_to_tdb(*args): - return _jplephem.utc_to_tdb(*args) + return _jplephem.utc_to_tdb(*args) utc_to_tdb = _jplephem.utc_to_tdb def utc_to_last(*args): - return _jplephem.utc_to_last(*args) + return _jplephem.utc_to_last(*args) utc_to_last = _jplephem.utc_to_last def last_to_utc(*args): - return _jplephem.last_to_utc(*args) + return _jplephem.last_to_utc(*args) last_to_utc = _jplephem.last_to_utc def epoch_to_j2000(*args): - return _jplephem.epoch_to_j2000(*args) + return _jplephem.epoch_to_j2000(*args) epoch_to_j2000 = _jplephem.epoch_to_j2000 def j2000_to_epoch(*args): - return _jplephem.j2000_to_epoch(*args) + return _jplephem.j2000_to_epoch(*args) j2000_to_epoch = _jplephem.j2000_to_epoch def add_aberration(*args): - return _jplephem.add_aberration(*args) + return _jplephem.add_aberration(*args) add_aberration = _jplephem.add_aberration def remove_aberration(*args): - return _jplephem.remove_aberration(*args) + return _jplephem.remove_aberration(*args) remove_aberration = _jplephem.remove_aberration def set_observer_coordinates(*args): - return _jplephem.set_observer_coordinates(*args) + return _jplephem.set_observer_coordinates(*args) set_observer_coordinates = _jplephem.set_observer_coordinates def geocentric_observer_track(*args): - return _jplephem.geocentric_observer_track(*args) + return _jplephem.geocentric_observer_track(*args) geocentric_observer_track = _jplephem.geocentric_observer_track def barycentric_observer_track(*args): - return _jplephem.barycentric_observer_track(*args) + return _jplephem.barycentric_observer_track(*args) barycentric_observer_track = _jplephem.barycentric_observer_track def barycentric_earth_track(*args): - return _jplephem.barycentric_earth_track(*args) + return _jplephem.barycentric_earth_track(*args) barycentric_earth_track = _jplephem.barycentric_earth_track def barycentric_object_track(*args): - return _jplephem.barycentric_object_track(*args) + return _jplephem.barycentric_object_track(*args) barycentric_object_track = _jplephem.barycentric_object_track # This file is compatible with both classic and new-style classes. - - diff --git a/utils/SSEphem/update_ssephem.py b/utils/SSEphem/update_ssephem.py index ce580e3..2e7a6ca 100644 --- a/utils/SSEphem/update_ssephem.py +++ b/utils/SSEphem/update_ssephem.py @@ -1,81 +1,82 @@ +from __future__ import print_function import os import numpy as np def LeapSecUpdate(): - os.system('wget http://maia.usno.navy.mil/ser7/leapsec.dat') - if os.access('leapsec.tab', os.F_OK): - os.system('mv leapsec.tab leapsec_old.tab') - try: - f = open('leapsec.dat','r') - lines = f.readlines() - fo = open('leapsec.tab','w') - for line in lines: - cos = line.split() - if cos[1] == 'JAN': - date = cos[0]+' January 1 ' - else: - date = cos[0]+' July 1 ' - jd = float(cos[4]) - leap = float(cos[6]) - leap = int(np.around(leap)) - nline = ' '+str(int(np.around(jd-2400000.5)))+' '+date+'TAI-UTC = '+str(leap)+'.0\n' - fo.write(nline) - f.close() - fo.close() - os.system('rm leapsec.dat') - except: - print 'No luck...' - os.system('rm leapsec.dat') - os.system('mv leapsec_old.tab leapsec.tab') + os.system('wget http://maia.usno.navy.mil/ser7/leapsec.dat') + if os.access('leapsec.tab', os.F_OK): + os.system('mv leapsec.tab leapsec_old.tab') + try: + f = open('leapsec.dat','r') + lines = f.readlines() + fo = open('leapsec.tab','w') + for line in lines: + cos = line.split() + if cos[1] == 'JAN': + date = cos[0]+' January 1 ' + else: + date = cos[0]+' July 1 ' + jd = float(cos[4]) + leap = float(cos[6]) + leap = int(np.around(leap)) + nline = ' '+str(int(np.around(jd-2400000.5)))+' '+date+'TAI-UTC = '+str(leap)+'.0\n' + fo.write(nline) + f.close() + fo.close() + os.system('rm leapsec.dat') + except: + print('No luck...') + os.system('rm leapsec.dat') + os.system('mv leapsec_old.tab leapsec.tab') def SSEphemDownload(): - if os.access('DEc403',os.F_OK): - os.system('mv DEc403 DEc403_old') - if os.access('ascp2000.403', os.F_OK): - os.system('mv ascp2000.403 ascp2000_old.403') - os.system('wget ftp://ssd.jpl.nasa.gov/pub/eph/planets/ascii/de403/ascp2000.403') - if os.access('header.403', os.F_OK): - os.system('mv header.403 header_old.403') - os.system('wget ftp://ssd.jpl.nasa.gov/pub/eph/planets/ascii/de403/header.403') - if os.access('ascp2000.403', os.F_OK) == False or os.access('header.403', os.F_OK) == False: - print 'No Luck...' - os.system('mv ascp2000_old.403 ascp2000.403') - os.system('mv header_old.403 header.403') - else: - os.system('cat header.403 ascp2000.403 > asc_cat.403') - os.system('./asc2bin asc_cat.403 2451544 2484394') - if os.access('DEc403',os.F_OK) == False: - print 'No Luck...' - if os.access('DEc403_old',os.F_OK): - os.system('mv DEc403_old DEc403') + if os.access('DEc403',os.F_OK): + os.system('mv DEc403 DEc403_old') + if os.access('ascp2000.403', os.F_OK): + os.system('mv ascp2000.403 ascp2000_old.403') + os.system('wget ftp://ssd.jpl.nasa.gov/pub/eph/planets/ascii/de403/ascp2000.403') + if os.access('header.403', os.F_OK): + os.system('mv header.403 header_old.403') + os.system('wget ftp://ssd.jpl.nasa.gov/pub/eph/planets/ascii/de403/header.403') + if os.access('ascp2000.403', os.F_OK) == False or os.access('header.403', os.F_OK) == False: + print('No Luck...') + os.system('mv ascp2000_old.403 ascp2000.403') + os.system('mv header_old.403 header.403') + else: + os.system('cat header.403 ascp2000.403 > asc_cat.403') + os.system('./asc2bin asc_cat.403 2451544 2484394') + if os.access('DEc403',os.F_OK) == False: + print('No Luck...') + if os.access('DEc403_old',os.F_OK): + os.system('mv DEc403_old DEc403') def IersUpdate(): - if os.access('finals2000A.data',os.F_OK): - os.system('mv finals2000A.data finals2000A_old.data') - os.system('wget http://maia.usno.navy.mil/ser7/finals2000A.data') - if os.access('finals2000A.data',os.F_OK) == False: - print 'one' - print 'No Luck...' - os.system('mv finals2000A_old.data finals2000A.data') - pass - else: - if os.access('iers.tab',os.F_OK): - os.system('mv iers.tab iers_old.tab') - try: - output = open('iers.tab','w') - finaldata = open('finals2000A.data','r') - for line in finaldata: - mj = line[7:15] - if len(line.split()) > 5: - c1 = line[18:27] - c2 = line[37:46] - c3 = line[58:68] - l = ' '+mj+' '+c1+' '+c2+' '+c3+' '+'\n' - output.write(l) - finaldata.close() - output.close() - except: - print 'two' - print 'No Luck...' - if os.access('iers_old.tab',os.F_OK): - os.system('mv iers_old.tab iers.tab') + if os.access('finals2000A.data',os.F_OK): + os.system('mv finals2000A.data finals2000A_old.data') + os.system('wget http://maia.usno.navy.mil/ser7/finals2000A.data') + if os.access('finals2000A.data',os.F_OK) == False: + print('one') + print('No Luck...') + os.system('mv finals2000A_old.data finals2000A.data') + pass + else: + if os.access('iers.tab',os.F_OK): + os.system('mv iers.tab iers_old.tab') + try: + output = open('iers.tab','w') + finaldata = open('finals2000A.data','r') + for line in finaldata: + mj = line[7:15] + if len(line.split()) > 5: + c1 = line[18:27] + c2 = line[37:46] + c3 = line[58:68] + l = ' '+mj+' '+c1+' '+c2+' '+c3+' '+'\n' + output.write(l) + finaldata.close() + output.close() + except: + print('two') + print('No Luck...') + if os.access('iers_old.tab',os.F_OK): + os.system('mv iers_old.tab iers.tab') diff --git a/uves/uvespipe.py b/uves/uvespipe.py index 7134800..142a26e 100644 --- a/uves/uvespipe.py +++ b/uves/uvespipe.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use("Agg") @@ -12,7 +13,7 @@ baryc_dir= base+'utils/SSEphem/' sys.path.append(baryc_dir) ephemeris='DEc403' - + import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages @@ -89,7 +90,7 @@ force_flat_nor = False force_thar_extract = False force_thar_wavcal = False -force_tharxc = False +force_tharxc = False force_sci_extract = False force_spectral_file_build = True force_stellar_pars = False @@ -111,24 +112,24 @@ N_Marsh = 3 # grado polinomio if template == 'UVES_red_obs_exp': - min_extract_col = 0 - min_extract_cols1 = [920,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - min_extract_cols2 = [1710,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - max_extract_col = 4000 - max_extract_cols1 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895] - max_extract_cols2 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3390] + min_extract_col = 0 + min_extract_cols1 = [920,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + min_extract_cols2 = [1710,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + max_extract_col = 4000 + max_extract_cols1 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895] + max_extract_cols2 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3390] elif template == 'UVES_dic2_obs_expfree': - min_extract_col = 0 - min_extract_cols1 = [920 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - min_extract_cols2 = [1710,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - max_extract_col = 4000 - max_extract_cols1 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895] - max_extract_cols2 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3390] + min_extract_col = 0 + min_extract_cols1 = [920 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + min_extract_cols2 = [1710,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + max_extract_col = 4000 + max_extract_cols1 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895] + max_extract_cols2 = [3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3895,3390] xbinning = 1 ybinning = 1 -ext_aperture = int(float(ext_aperture)/float(ybinning)) +ext_aperture = int(float(ext_aperture)/float(ybinning)) ncoef_x = 5 ncoef_m = 6 @@ -139,60 +140,60 @@ ############################# -print "\n\n\tUVES @VLT PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tUVES @VLT PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # file containing the log log = dirout+'night.log' biases, flats, orderref, ThAr_ref, sim_sci, ThAr_ref_dates, obnames, exptimes = uvesutils.FileClassify(dirin,log) -print '\tThis in the log of the night:\n' +print('\tThis in the log of the night:\n') f = open(log) flines = f.readlines() for line in flines: - print '\t\t'+line[:-1] -print '\n' + print('\t\t'+line[:-1]) +print('\n') if ((os.access(dirout+'Flat.fits',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): # median combine list of flats - print "\t\tGenerating Master calibration frames..." + print("\t\tGenerating Master calibration frames...") MasterBias, RO_bias, GA_bias = uvesutils.MedianCombine(biases) hdu = pyfits.PrimaryHDU(MasterBias) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> MasterBias: done!" + print("\t\t-> MasterBias: done!") Flat, RO_fl, GA_fl = uvesutils.MedianCombine(flats,bias=MasterBias) hdu = pyfits.PrimaryHDU(Flat) if (os.access(dirout+'Flat.fits',os.F_OK)): os.remove(dirout+'Flat.fits') hdu.writeto(dirout+'Flat.fits') - print "\t\t-> MasterFlat: done!" + print("\t\t-> MasterFlat: done!") hdu = pyfits.open(orderref[0]) d1 = hdu[1].data - MasterBias[:,:,0] d2 = hdu[2].data - MasterBias[:,:,1] d1 = d1.T d2 = d2.T - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") c_all1, nord1 = GLOBALutils.get_them(d1,int(0.2*ext_aperture),trace_degree,mode=1,nsigmas=10) - print '\t\t'+str(nord1)+' orders for chip1 found...' + print('\t\t'+str(nord1)+' orders for chip1 found...') c_all2, nord2 = GLOBALutils.get_them(d2,int(0.2*ext_aperture),trace_degree,mode=1,nsigmas=10, endat=2030) - print '\t\t'+str(nord2)+' orders for chip2 found...' + print('\t\t'+str(nord2)+' orders for chip2 found...') trace_dict = {'c_all1':c_all1,'nord1':nord1,\ 'c_all2':c_all2,'nord2':nord2,\ @@ -215,7 +216,7 @@ Flat = pyfits.getdata(dirout+'Flat.fits') MasterBias = pyfits.getdata(dirout+'MasterBias.fits') -print '\n\tNormalization of Flat calibration frame:' +print('\n\tNormalization of Flat calibration frame:') # Normalization of flat frame if (os.access(dirout+'NFlat.fits',os.F_OK)==False) or (os.access(dirout+'Flat_spec.pkl',os.F_OK)==False) or force_flat_nor: @@ -235,21 +236,21 @@ hdu.writeto(dirout+'NFlat.fits') pickle.dump( sflat, open( dirout+'Flat_spec.pkl', 'w' ) ) - + else: nflat = pyfits.getdata(dirout+'NFlat.fits') sflat = pickle.load( open( dirout+'Flat_spec.pkl' ) ) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') for fsim in ThAr_ref: - print fsim + print(fsim) thar_fits = dirout + fsim.split('/')[-1][:-4]+'spec.pkl' if ( os.access(thar_fits,os.F_OK) == False ) or ( force_thar_extract ): hthar = pyfits.open( fsim ) dthar = (np.dstack((hthar[1].data,hthar[2].data)) - MasterBias) / nflat - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") dthar1 = dthar[:,:,0].T - dthar2 = dthar[:,:,1].T + dthar2 = dthar[:,:,1].T thar1_S = GLOBALutils.simple_extraction(dthar1,c_all1,ext_aperture,min_extract_cols1,max_extract_cols1,npools) thar2_S = GLOBALutils.simple_extraction(dthar2,c_all2,ext_aperture,min_extract_cols2,max_extract_cols2,npools) @@ -259,9 +260,9 @@ pickle.dump( thar_S, open( thar_fits, 'w' ) ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") for fsim in ThAr_ref: hthar = pyfits.open( fsim ) @@ -270,7 +271,7 @@ if ( os.access(wavsol_pkl,os.F_OK) == False ) or ( force_thar_wavcal ): - print "\t\tWorking on initial ThAr file", fsim + print("\t\tWorking on initial ThAr file", fsim) mjd, mjd0 = uvesutils.mjd_fromheader2( hthar[0].header ) thar_hd = hthar[0].header thar_S = pickle.load(open(thar_fits,'r')) @@ -304,7 +305,7 @@ minlines=20,FixEnds=False,Dump_Argon=dumpargon,sigmai=2.,\ Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,do_xc=False, line_width=6,pixelization=True) - if order == 7: + if order == 7: Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*(len(thar_order)-1), len(thar_order)) All_Pixel_Centers = np.append( All_Pixel_Centers, pixel_centers ) @@ -315,7 +316,7 @@ All_Intensities = np.append( All_Intensities, intensities ) p0 = np.zeros( npar_wsol ) - p0[0] = (7+ro0) * Global_ZP + p0[0] = (7+ro0) * Global_ZP p1, G_pix, G_ord, G_wav, II, rms_ms, G_res = \ GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers, All_Wavelengths, All_Orders,\ np.ones(All_Intensities.shape), p0, Cheby=use_cheby,\ @@ -356,12 +357,12 @@ coeffs_pix2wav, coeffs_pix2sigma, pixel_centers, wavelengths,\ rms_ms, residuals, centroids, sigmas, intensities =\ - GLOBALutils.Initial_Wav_Calibration(order_dir+'nrorder_'+\ - order_s+'.iwdat', thar_order, order, wei, rmsmax=100, \ - minlines=20,FixEnds=False,Dump_Argon=dumpargon,sigmai=2.,\ - Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,do_xc=False, line_width=6,pixelization=True) + GLOBALutils.Initial_Wav_Calibration(order_dir+'nrorder_'+\ + order_s+'.iwdat', thar_order, order, wei, rmsmax=100, \ + minlines=20,FixEnds=False,Dump_Argon=dumpargon,sigmai=2.,\ + Dump_AllLines=True, Cheby=use_cheby,porder=ncoef_x,do_xc=False, line_width=6,pixelization=True) - if order == 11: + if order == 11: Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*(len(thar_order)-1), len(thar_order)) All_Pixel_Centers2 = np.append( All_Pixel_Centers2, pixel_centers ) @@ -370,23 +371,23 @@ All_Centroids2 = np.append( All_Centroids2, centroids) All_Sigmas2 = np.append( All_Sigmas2, sigmas) All_Intensities2 = np.append( All_Intensities2, intensities ) - - #isz = pixel_centers - sigmas - #der = pixel_centers + sigmas - #isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz, len(thar_order)) - #der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der, len(thar_order)) - #sig = 0.5*(der-isz) - #fwhm = 2.35 * sig - #resol = wavelengths / fwhm - #print order, np.median(resol) - #plot(pixel_centers,resol,'o') - #show() + + #isz = pixel_centers - sigmas + #der = pixel_centers + sigmas + #isz = GLOBALutils.Cheby_eval( coeffs_pix2wav, isz, len(thar_order)) + #der = GLOBALutils.Cheby_eval( coeffs_pix2wav, der, len(thar_order)) + #sig = 0.5*(der-isz) + #fwhm = 2.35 * sig + #resol = wavelengths / fwhm + #print order, np.median(resol) + #plot(pixel_centers,resol,'o') + #show() #print dfgh p0 = np.zeros( npar_wsol ) - p0[0] = (11 + ro0 + len(c_all1) + order_gap) * Global_ZP + p0[0] = (11 + ro0 + len(c_all1) + order_gap) * Global_ZP p2, G_pix2, G_ord2, G_wav2, II2, rms_ms2, G_res2 = \ - GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers2, All_Wavelengths2, All_Orders2,\ + GLOBALutils.Fit_Global_Wav_Solution(All_Pixel_Centers2, All_Wavelengths2, All_Orders2,\ np.ones(All_Intensities2.shape), p0, Cheby=use_cheby,\ maxrms=MRMS, Inv=Inverse_m,minlines=400,order0=ro0 + len(c_all1) + order_gap, \ ntotal=nord2,npix=len(thar_order),nx=ncoef_x,nm=ncoef_m) @@ -413,21 +414,21 @@ pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) -print '\n\tThe following targets will be processed:' +print('\n\tThe following targets will be processed:') new_list = [] if object2do == 'all': - new_list = sim_sci + new_list = sim_sci else: - for fsim in sim_sci: - hd = pyfits.getheader(fsim) - if object2do in hd['OBJECT']: - new_list.append(fsim) + for fsim in sim_sci: + hd = pyfits.getheader(fsim) + if object2do in hd['OBJECT']: + new_list.append(fsim) for fsim in new_list: - hd = pyfits.getheader(fsim) - print '\t\t'+str(hd['OBJECT']) + hd = pyfits.getheader(fsim) + print('\t\t'+str(hd['OBJECT'])) # Does any image have a special requirement for dealing with the moonlight? if os.access(dirin + 'moon_corr.txt', os.F_OK): @@ -450,8 +451,8 @@ # Extract all Science files for fsim in new_list: - print '\n' - print "\t--> Working on image: ", fsim + print('\n') + print("\t--> Working on image: ", fsim) know_moon = False if fsim.split('/')[-1] in spec_moon: @@ -460,14 +461,14 @@ here_moon = use_moon[I] h = pyfits.open(fsim) - mjd,mjd0 = uvesutils.mjd_fromheader2(h[0].header) + mjd,mjd0 = uvesutils.mjd_fromheader2(h[0].header) ron1,gain1 = float(h[1].header['HIERARCH ESO DET OUT1 RON']),float(h[1].header['HIERARCH ESO DET OUT1 GAIN']) ron2,gain2 = float(h[2].header['HIERARCH ESO DET OUT1 RON']),float(h[2].header['HIERARCH ESO DET OUT1 GAIN']) # Object name obname = h[0].header['OBJECT'].replace(' ','') - print "\t\tObject name:",obname + print("\t\tObject name:",obname) # Open file, trim, overscan subtract and MasterBias subtract bac_fits = dirout + 'BAC_' + fsim.split('/')[-1][:-4]+'.fits' sky_fits = dirout + 'SKY_' + fsim.split('/')[-1][:-4]+'.fits' @@ -484,7 +485,7 @@ for i in range(len(c_all2)): centers2[i] = np.polyval(c_all2[i],np.arange(data.shape[0])) - print '\t\tEstimating background of scattered light...' + print('\t\tEstimating background of scattered light...') force_bac=False if (os.access(bac_fits,os.F_OK) == False) or force_bac: bac1 = GLOBALutils.get_scat(data[:,:,0].T,centers1,span=sky_aperture) @@ -511,7 +512,7 @@ else: sky = pyfits.getdata(sky_fits) - data = data - sky + data = data - sky data = data / nflat ra = float(h[0].header['RA']) @@ -522,7 +523,7 @@ ra = ra2 dec = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') # set observatory parameters altitude = float(h[0].header['ESO TEL GEOELEV']) @@ -541,16 +542,16 @@ lbary_ltopo = 1.0 + res['frac'][0] bcvel_baryc = ( lbary_ltopo - 1.0 ) * 2.99792458E5 - print "\t\tBarycentric velocity:", bcvel_baryc + print("\t\tBarycentric velocity:", bcvel_baryc) res = jplephem.pulse_delay(ra/15.0, dec, int(mjd), mjd%1, 1, 0.0) mbjd = mjd + res['delay'][0] / (3600.0 * 24.0) # Moon Phase Calculations - gobs = ephem.Observer() + gobs = ephem.Observer() gobs.name = 'VLT' - gobs.lat = rad(latitude) # lat/long in decimal degrees - gobs.long = rad(longitude) + gobs.lat = rad(latitude) # lat/long in decimal degrees + gobs.long = rad(longitude) gobs.date = h[0].header['DATE-OBS'].replace('T',' ') mephem = ephem.Moon() mephem.compute(gobs) @@ -560,7 +561,7 @@ res = jplephem.object_doppler("Moon", int(mjd), mjd%1, 1, 0.0) lunation,moon_state,moonsep,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,ra,dec) refvel = bcvel_baryc + moonvel - print '\t\tRadial Velocity of sacttered moonlight:',refvel + print('\t\tRadial Velocity of sacttered moonlight:',refvel) sci_fits = dirout + fsim.split('/')[-1][:-4]+'spec.pkl' sci_fits_simple = dirout + fsim.split('/')[-1][:-4]+'spec.simple.pkl' @@ -568,29 +569,29 @@ if ( os.access(sci_fits,os.F_OK) == False ) or ( os.access(sci_fits_simple,os.F_OK) == False ) or \ ( force_sci_extract ): - print "\t\tNo previous extraction or extraction forced for science file", fsim, "extracting..." + print("\t\tNo previous extraction or extraction forced for science file", fsim, "extracting...") #""" - print "\t\t\tweights for chip1..." + print("\t\t\tweights for chip1...") P1 = GLOBALutils.obtain_P(data[:,:,0].T,c_all1,ext_aperture,ron1,gain1,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_cols1,max_extract_cols1, npools) - print "\t\t\tweights for chip2..." + print("\t\t\tweights for chip2...") P2 = GLOBALutils.obtain_P(data[:,:,1].T,c_all2,ext_aperture,ron2,gain2,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_cols2,max_extract_cols2, npools) P = np.dstack((P1,P2)) #""" #P = pyfits.getdata(P_fits) - print "\t\t\tsimple extraction for chip1..." + print("\t\t\tsimple extraction for chip1...") sci_Ss1 = GLOBALutils.simple_extraction(data[:,:,0].T,c_all1,ext_aperture,min_extract_cols1,max_extract_cols1,npools) - print "\t\t\tsimple extraction for chip2..." + print("\t\t\tsimple extraction for chip2...") sci_Ss2 = GLOBALutils.simple_extraction(data[:,:,1].T,c_all2,ext_aperture,min_extract_cols2,max_extract_cols2,npools) - + sci_Ss1 = GLOBALutils.invert(sci_Ss1) sci_Ss2 = GLOBALutils.invert(sci_Ss2) #""" - print "\t\t\t\optimal extraction for chip1..." + print("\t\t\t\optimal extraction for chip1...") sci_S1 = GLOBALutils.optimal_extraction(data[:,:,0].T,P1,c_all1,ext_aperture,ron1,gain1,S_Marsh,NCosmic_Marsh,\ - min_extract_cols1,max_extract_cols1,npools) - print "\t\t\t\optimal extraction for chip2..." + min_extract_cols1,max_extract_cols1,npools) + print("\t\t\t\optimal extraction for chip2...") sci_S2 = GLOBALutils.optimal_extraction(data[:,:,1].T,P2,c_all2,ext_aperture,ron2,gain2,S_Marsh,NCosmic_Marsh,\ min_extract_cols2,max_extract_cols2,npools) #""" @@ -600,19 +601,19 @@ sci_S1 = GLOBALutils.invert(sci_S1) sci_S2 = GLOBALutils.invert(sci_S2) - + if (os.access(P_fits,os.F_OK)): os.remove( P_fits ) hdu = pyfits.PrimaryHDU( P ) hdu.writeto( P_fits ) sci_S = {'chip1':sci_S1, 'chip2':sci_S2} pickle.dump(sci_S,open(sci_fits,'w')) - + sci_Ss = {'chip1':sci_Ss1, 'chip2':sci_Ss2} pickle.dump(sci_Ss,open(sci_fits_simple,'w')) - + else: - print '\t\t'+fsim+" has already been extracted, reading in product fits files..." + print('\t\t'+fsim+" has already been extracted, reading in product fits files...") sci_S = pickle.load(open(sci_fits,'r')) sci_Ss = pickle.load(open(sci_fits_simple,'r')) @@ -627,7 +628,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH SHUTTER START UT', h[0].header['DATE-OBS'].split('T')[1]) hdu = GLOBALutils.update_header(hdu,'HIERARCH TEXP (S)',h[0].header['EXPTIME']) hdu = GLOBALutils.update_header(hdu,'HIERARCH BARYCENTRIC CORRECTION (KM/S)', bcvel_baryc) - hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) + hdu = GLOBALutils.update_header(hdu,'HIERARCH (LAMBDA_BARY / LAMBDA_TOPO)', lbary_ltopo) hdu = GLOBALutils.update_header(hdu,'HIERARCH TARGET NAME', obname) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',h[0].header['RA']) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',h[0].header['DEC']) @@ -644,18 +645,18 @@ # get ThAr closest in time im = np.argmin(np.absolute(np.array(ThAr_ref_dates) - mjd)) pkl_wsol = dirout + ThAr_ref[im].split('/')[-1][:-4]+'spec.wavsolpars.pkl' - print "\t\tUnpickling wavelength solution from", pkl_wsol, " ..." + print("\t\tUnpickling wavelength solution from", pkl_wsol, " ...") wsol_dict = pickle.load(open(pkl_wsol,'r')) # Apply new wavelength solution including barycentric correction - equis = np.arange( data.shape[0] ) + equis = np.arange( data.shape[0] ) for order in range(nord1): #order = 16 #print order m = order + ro0 chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[0],\ - order0=ro0, ntotal=nord1, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) + order0=ro0, ntotal=nord1, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) + WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p1'],chebs,ncoef_x,ncoef_m) spec[0,order,:] = GLOBALutils.ToVacuum(WavSol) spec[1,order,:] = sci_S['chip1'][order,1,:] #spec[1,order,:300] = 0. @@ -684,7 +685,7 @@ nI = np.where(np.isnan(spec[5,order])==False)[0] nJ = np.where(np.isnan(spec[5,order])==True)[0] spec[6,order,:][L] = spec[8,order,:][L] ** 2 - spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) + spec[9,order,:][L] = spec[5,order,:][L] * (dlambda_dx[L] ** 1) spec[10,order,:][L] = spec[6,order,:][L] / (dlambda_dx[L] ** 2) @@ -693,7 +694,7 @@ m = order + ro0 + nord1 + order_gap chebs = GLOBALutils.Calculate_chebs(equis, m, npix=data.shape[0],\ order0=ro0 + nord1 + order_gap, ntotal=nord2, Inverse=Inverse_m,nx=ncoef_x,nm=ncoef_m) - WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p2'],chebs,ncoef_x,ncoef_m) + WavSol = lbary_ltopo * (1.0/m) * GLOBALutils.Joint_Polynomial_Cheby(wsol_dict['p2'],chebs,ncoef_x,ncoef_m) spec[0,order2,:] = GLOBALutils.ToVacuum(WavSol) spec[1,order2,:] = sci_S['chip2'][order,1,:] #spec[1,order2,:300] = 0. @@ -723,27 +724,27 @@ nI = np.where(np.isnan(spec[5,order2])==False)[0] nJ = np.where(np.isnan(spec[5,order2])==True)[0] spec[6,order2,:][L] = spec[8,order2,:][L] ** 2 - spec[9,order2,:][L] = spec[5,order2,:][L] * (dlambda_dx[L] ** 1) + spec[9,order2,:][L] = spec[5,order2,:][L] * (dlambda_dx[L] ** 1) spec[10,order2,:][L] = spec[6,order2,:][L] / (dlambda_dx[L] ** 2) if (not JustExtract): if DoClass: # spectral analysis - print "\t\tSpectral Analysis..." + print("\t\tSpectral Analysis...") query_success = False # First, query SIMBAD with the object name query_success,sp_type_query = GLOBALutils.simbad_query_obname(obname) # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu = GLOBALutils.update_header(hdu,'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + fsim.split('/')[-1][:-4]+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") Rx = np.around(1./np.sqrt(1./40000.**2 - 1./ref_RES**2)) spec2 = spec.copy() for i in range(spec.shape[1]): @@ -754,12 +755,12 @@ f = open(pars_file,'w') f.write(line) f.close() - + else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -775,19 +776,19 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH VSINI', vsini) hdu = GLOBALutils.update_header(hdu,'HIERARCH VEL0', vel0) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." - + print("\t\t\tWill use",sp_type,"mask for CCF.") + velw = 300 velsh = 3. - + # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) mh_v = GLOBALutils.ToVacuum( mh ) - + # make mask larger accounting for factor ~2.5 lower res in FEROS w/r to HARPS av_m = 0.5*( ml_v + mh_v ) ml_v -= 1.5*(av_m - ml_v) @@ -806,9 +807,9 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide - print '\t\t\tComputing the CCF...' + print('\t\t\tComputing the CCF...') cond = True while (cond): # first rough correlation to find the minimum @@ -826,17 +827,17 @@ rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ GLOBALutils.XCor(spec, ml_v, mh_v, weight, vel0_xc, lbary_ltopo, vel_width=vel_width,vel_step=0.2,\ spec_order=9,iv_order=10,sn_order=8,max_vel_rough=velw) - + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf) pred = scipy.interpolate.splev(vels,tck1) xc_av /= pred - + if sp_type == 'M5': moon_sig = 2.5 elif sp_type == 'K5': @@ -865,11 +866,11 @@ #print p1gau[1] if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -877,13 +878,13 @@ BSerr = -999.00 xc_dict = {'vels':vels,'xc_av':xc_av,'XCmodelgau':XCmodelgau,'Ls2':Ls2,'refvel':refvel,\ - 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ - 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ - 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ - 'XCmodelgau_m':XCmodelgau_m} + 'rvels':rvels,'rxc_av':rxc_av,'rpred':rpred,'rxc_av_orig':rxc_av_orig,\ + 'rvel0_xc':rvel0_xc,'xc_full':xc_full, 'p1':p1, 'sn':sn, 'p1gau':p1gau,\ + 'p1_m':p1_m,'XCmodel_m':XCmodel_m,'p1gau_m':p1gau_m,'Ls2_m':Ls2_m,\ + 'XCmodelgau_m':XCmodelgau_m} moon_dict = {'moonmatters':moonmatters,'moon_state':moon_state,'moonsep':moonsep,\ - 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} + 'lunation':lunation,'mephem':mephem,'texp':h[0].header['EXPTIME']} pkl_xc = dirout + fsim.split('/')[-1][:-4]+obname+'_XC_'+sp_type+'.pkl' @@ -897,7 +898,7 @@ airmass = h[0].header['ESO OBS AIRM'] seeing = -999 - TEXP = h[0].header['EXPTIME'] + TEXP = h[0].header['EXPTIME'] SNR_5130 = np.median(spec[8,13,1900:2101] ) if sp_type=='G2': @@ -917,12 +918,12 @@ BSerr = D / float(np.round(SNR_5130)) + C RVerr = 0.5 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = np.around(RVerr,4) BSerr = np.around(BSerr,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -949,10 +950,10 @@ hdu = GLOBALutils.update_header(hdu,'BJD_OUT', bjd_out) line_out = "%-15s %18.8f %9.4f %7.4f %9.3f %5.3f uves ceres %8d %6d %5.2f %5.2f %5.1f %4.2f %5.2f %6.1f %4d %s\n"%\ (obname, bjd_out, RV, RVerr2, BS, BSerr, ref_RES, T_eff_epoch, logg_epoch, Z_epoch, vsini_epoch, XC_min, disp_epoch,\ - TEXP, SNR_5130_R, ccf_pdf) + TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) if (os.access( dirout + fout,os.F_OK)): os.remove( dirout + fout) hdu.writeto( dirout + fout ) -f_res.close() \ No newline at end of file +f_res.close() diff --git a/uves/uvesutils.py b/uves/uvesutils.py index a0ea679..2c9cdf7 100644 --- a/uves/uvesutils.py +++ b/uves/uvesutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import matplotlib matplotlib.use("Agg") import numpy as np @@ -18,7 +19,7 @@ def FileClassify(diri, log): """ - + Classifies all files in a directory and writes a night log of science images """ @@ -39,50 +40,50 @@ def FileClassify(diri, log): #Do not consider the images specified in dir+badfiles.txt bad_files = [] if os.access(diri+'bad_files.txt',os.F_OK): - bf = open(diri+'bad_files.txt') - linesbf = bf.readlines() - for line in linesbf: - bad_files.append(diri+line[:-1]) - bf.close() - + bf = open(diri+'bad_files.txt') + linesbf = bf.readlines() + for line in linesbf: + bad_files.append(diri+line[:-1]) + bf.close() + all_files = glob.glob(diri+"/*fits") for archivo in all_files: - #print archivo - dump = False - for bf in bad_files: - if archivo == bf: - dump = True - break - if dump == False: - h = pyfits.open(archivo) - print archivo, h[0].header['HIERARCH ESO DPR CATG'],h[0].header['HIERARCH ESO DPR TYPE'] - if 'SCIENCE' in h[0].header['HIERARCH ESO DPR CATG']: - science.append(archivo) - obname = h[0].header['OBJECT'] - obnames.append( obname ) - ra = h[0].header['RA'] - delta = h[0].header['DEC'] - airmass= h[0].header['HIERARCH ESO TEL AIRM START'] - texp = h[0].header['EXPTIME'] - date = h[0].header['DATE-OBS'].split('T')[0] - hour = h[0].header['DATE-OBS'].split('T')[1] - exptimes.append( texp ) - #rint obname, ra, delta, texp, airmass, date, hour, archivo - line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, float(texp), float(airmass), date, hour, archivo) - f.write(line) - elif 'CALIB' in h[0].header['HIERARCH ESO DPR CATG']: - if h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,FLAT': - flats.append(archivo) - mjd, mjd0 = mjd_fromheader2(h[0].header) - flat_ref_dates.append( mjd ) - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,WAVE': - ThAr_ref.append(archivo) - mjd, mjd0 = mjd_fromheader2(h[0].header) - ThAr_ref_dates.append( mjd ) - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,ORDERDEF': - orderref.append(archivo) - elif h[0].header['HIERARCH ESO DPR TYPE'] == 'BIAS': - biases.append(archivo) + #print archivo + dump = False + for bf in bad_files: + if archivo == bf: + dump = True + break + if dump == False: + h = pyfits.open(archivo) + print(archivo, h[0].header['HIERARCH ESO DPR CATG'],h[0].header['HIERARCH ESO DPR TYPE']) + if 'SCIENCE' in h[0].header['HIERARCH ESO DPR CATG']: + science.append(archivo) + obname = h[0].header['OBJECT'] + obnames.append( obname ) + ra = h[0].header['RA'] + delta = h[0].header['DEC'] + airmass= h[0].header['HIERARCH ESO TEL AIRM START'] + texp = h[0].header['EXPTIME'] + date = h[0].header['DATE-OBS'].split('T')[0] + hour = h[0].header['DATE-OBS'].split('T')[1] + exptimes.append( texp ) + #rint obname, ra, delta, texp, airmass, date, hour, archivo + line = "%-15s %10s %10s %8.2f %4.2f %8s %11s %s\n" % (obname, ra, delta, float(texp), float(airmass), date, hour, archivo) + f.write(line) + elif 'CALIB' in h[0].header['HIERARCH ESO DPR CATG']: + if h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,FLAT': + flats.append(archivo) + mjd, mjd0 = mjd_fromheader2(h[0].header) + flat_ref_dates.append( mjd ) + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,WAVE': + ThAr_ref.append(archivo) + mjd, mjd0 = mjd_fromheader2(h[0].header) + ThAr_ref_dates.append( mjd ) + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'LAMP,ORDERDEF': + orderref.append(archivo) + elif h[0].header['HIERARCH ESO DPR TYPE'] == 'BIAS': + biases.append(archivo) flat_ref_dates,ThAr_ref,ThAr_ref_dates = np.array(flat_ref_dates),np.array(ThAr_ref),np.array(ThAr_ref_dates) flats = np.array(flats) @@ -103,9 +104,9 @@ def mjd_fromheader2(hd): secinday = 24*3600.0 - date = hd['DATE-OBS'].split('T')[0] - ut = hd['DATE-OBS'].split('T')[1] - + date = hd['DATE-OBS'].split('T')[0] + ut = hd['DATE-OBS'].split('T')[1] + datetu = date.replace('-',':') mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:10])) @@ -128,12 +129,12 @@ def MedianCombine(ImgList, bias=0.): if n==0: raise ValueError("empty list provided!") - print 0, ImgList[0] + print(0, ImgList[0]) h = pyfits.open(ImgList[0]) d1 = h[1].data d2 = h[2].data - + factor = 1.25 if (n < 3): factor = 1 @@ -144,16 +145,16 @@ def MedianCombine(ImgList, bias=0.): ron1 = factor * ron1 / np.sqrt(n) ron2 = factor * ron2 / np.sqrt(n) if n>1: - for i in range(n-1): - print i,ImgList[i+1] - td = pyfits.open(ImgList[i+1]) - td = np.dstack((td[1].data,td[2].data)) - bias - d1 = np.dstack((d1,td[:,:,0])) - d2 = np.dstack((d2,td[:,:,1])) - d1 = np.median(d1,axis=2) - d2 = np.median(d2,axis=2) - - combined = np.dstack((d1,d2)) + for i in range(n-1): + print(i,ImgList[i+1]) + td = pyfits.open(ImgList[i+1]) + td = np.dstack((td[1].data,td[2].data)) - bias + d1 = np.dstack((d1,td[:,:,0])) + d2 = np.dstack((d2,td[:,:,1])) + d1 = np.median(d1,axis=2) + d2 = np.median(d2,axis=2) + + combined = np.dstack((d1,d2)) return combined, [ron1,ron2], [gain1,gain2] def OverscanTrim(d,ds): @@ -175,96 +176,96 @@ def OverscanTrim(d,ds): return newdata def bac_flat(flat,traces,ext): - ext = int(np.around(.5*ext)) - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - bac = np.zeros(flat.shape) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - for px in np.arange(flat.shape[0]): - cut = flat[px] - cut = scipy.signal.medfilt(cut,11) - refx,refy = [],[] - for i in np.arange(len(traces)-1)+1: - """ - if i == 0: - ncut = cut[peaks[i,px]-40:peaks[i,px]] - im = peaks[i,px]-40 + np.argmin(ncut) - refx.append(im) - refy.append(cut[im]) - """ - ncut = cut[peaks[i-1,px]:peaks[i,px]] - im = peaks[i-1,px] + np.argmin(ncut) - refx.append(im) - refy.append(cut[im]) - refx,refy = np.array(refx),np.array(refy) - tck = scipy.interpolate.splrep(refx,refy,k=1) - bac[px] = scipy.interpolate.splev(np.arange(flat.shape[1]),tck) - bac[px,:refx[0]] = refy[0] - bac[px,refx[-1]:] = refy[-1] - bac = scipy.signal.medfilt(bac,[11,1]) - return bac - + ext = int(np.around(.5*ext)) + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + bac = np.zeros(flat.shape) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + for px in np.arange(flat.shape[0]): + cut = flat[px] + cut = scipy.signal.medfilt(cut,11) + refx,refy = [],[] + for i in np.arange(len(traces)-1)+1: + """ + if i == 0: + ncut = cut[peaks[i,px]-40:peaks[i,px]] + im = peaks[i,px]-40 + np.argmin(ncut) + refx.append(im) + refy.append(cut[im]) + """ + ncut = cut[peaks[i-1,px]:peaks[i,px]] + im = peaks[i-1,px] + np.argmin(ncut) + refx.append(im) + refy.append(cut[im]) + refx,refy = np.array(refx),np.array(refy) + tck = scipy.interpolate.splrep(refx,refy,k=1) + bac[px] = scipy.interpolate.splev(np.arange(flat.shape[1]),tck) + bac[px,:refx[0]] = refy[0] + bac[px,refx[-1]:] = refy[-1] + bac = scipy.signal.medfilt(bac,[11,1]) + return bac + def ccd_flat(flat,traces,ext): - print ext - nflat = np.ones(flat.shape) - ext = int(np.around(ext)) - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - for px in np.arange(flat.shape[0]): - for py in range(len(traces)): - tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] - nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf - - return nflat - + print(ext) + nflat = np.ones(flat.shape) + ext = int(np.around(ext)) + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + for px in np.arange(flat.shape[0]): + for py in range(len(traces)): + tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] + nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf + + return nflat + def normalize_flat(flat,traces,ext): - nflat = np.ones(flat.shape) - ext = int(np.around(.5*ext))+2 - ejex = np.arange(flat.shape[0]) - peaks = np.zeros((len(traces),flat.shape[0])) - for i in range(len(traces)): - peaks[i] = np.around(np.polyval(traces[i],ejex)) - peaks = peaks.astype('int') - spec_flat = np.zeros((len(traces),flat.shape[0])) - - for px in np.arange(flat.shape[0]): - for py in range(len(traces)): - tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] - tf2 = flat[px,peaks[py,px]-int(np.around(ext/2.)):peaks[py,px]+int(np.around(ext/2.))+1] - nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf / np.median(tf) - spec_flat[py,px] = np.sum(tf2) - #for i in range(len(traces)): - # plot(spec_flat[i]) - # show() - for i in range(len(traces)): - I = np.where(np.isnan(spec_flat[i]))[0] - spec_flat[i][I] = 0. - ss = scipy.signal.medfilt(spec_flat[i],15) - spec_flat[i] /= ss.max() - - #spec_flat_o = spec_flat.copy() - #for i in range(len(traces)): - # spec_flat[i] = spec_flat[i]/scipy.signal.medfilt(spec_flat[i],301) - # spec_flat[i] = spec_flat[i]/sm.max() - return nflat,spec_flat + nflat = np.ones(flat.shape) + ext = int(np.around(.5*ext))+2 + ejex = np.arange(flat.shape[0]) + peaks = np.zeros((len(traces),flat.shape[0])) + for i in range(len(traces)): + peaks[i] = np.around(np.polyval(traces[i],ejex)) + peaks = peaks.astype('int') + spec_flat = np.zeros((len(traces),flat.shape[0])) + + for px in np.arange(flat.shape[0]): + for py in range(len(traces)): + tf = flat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] + tf2 = flat[px,peaks[py,px]-int(np.around(ext/2.)):peaks[py,px]+int(np.around(ext/2.))+1] + nflat[px,peaks[py,px]-ext:peaks[py,px]+ext+1] = tf / np.median(tf) + spec_flat[py,px] = np.sum(tf2) + #for i in range(len(traces)): + # plot(spec_flat[i]) + # show() + for i in range(len(traces)): + I = np.where(np.isnan(spec_flat[i]))[0] + spec_flat[i][I] = 0. + ss = scipy.signal.medfilt(spec_flat[i],15) + spec_flat[i] /= ss.max() + + #spec_flat_o = spec_flat.copy() + #for i in range(len(traces)): + # spec_flat[i] = spec_flat[i]/scipy.signal.medfilt(spec_flat[i],301) + # spec_flat[i] = spec_flat[i]/sm.max() + return nflat,spec_flat def get_sky(sc,lim,span1,span2, typ='median'): - sky = np.zeros(sc.shape) - ejeX = np.arange(sc.shape[0]) - - for y in range(sc.shape[1]): - lims = np.around(lim[:,y]).astype('int') - for j in range(len(lims)): - med1 = np.median(sc[lims[j]-span2:lims[j]-span1+1,y]) - med2 = np.median(sc[lims[j]+span1:lims[j]+span2+1,y]) - med = np.mean([med1,med2]) - sky[lims[j]-span2:lims[j]+span2+1,y] = med - - return sky \ No newline at end of file + sky = np.zeros(sc.shape) + ejeX = np.arange(sc.shape[0]) + + for y in range(sc.shape[1]): + lims = np.around(lim[:,y]).astype('int') + for j in range(len(lims)): + med1 = np.median(sc[lims[j]-span2:lims[j]-span1+1,y]) + med2 = np.median(sc[lims[j]+span1:lims[j]+span2+1,y]) + med = np.mean([med1,med2]) + sky[lims[j]-span2:lims[j]+span2+1,y] = med + + return sky diff --git a/uves/wavcals/make_linelist.py b/uves/wavcals/make_linelist.py index 8dde200..2360e33 100644 --- a/uves/wavcals/make_linelist.py +++ b/uves/wavcals/make_linelist.py @@ -1,30 +1,31 @@ +from __future__ import print_function from pylab import * refo = 3 while refo < 23: - srefo = str(refo) - if refo <10: - srefo = '0'+srefo + srefo = str(refo) + if refo <10: + srefo = '0'+srefo - d = np.loadtxt('all_lines.txt') - allw = d[:,1] + d = np.loadtxt('all_lines.txt') + allw = d[:,1] - d2 = np.loadtxt('rorder_'+srefo+'.iwdat') - p,w = d2[:,0],d2[:,1] - c = np.polyfit(w,p,4) - npx = np.polyval(c,allw) + d2 = np.loadtxt('rorder_'+srefo+'.iwdat') + p,w = d2[:,0],d2[:,1] + c = np.polyfit(w,p,4) + npx = np.polyval(c,allw) - I = np.where((npx>10) & (npx<4038))[0] - npx,nwv = npx[I],allw[I] + I = np.where((npx>10) & (npx<4038))[0] + npx,nwv = npx[I],allw[I] - f = open('nrorder_'+srefo+'.iwdat','w') - for i in range(len(npx)): - if nwv[i] > w[0] - 100 and nwv[i] < w[-1] + 100: - #print npx[i],nwv[i] - f.write('1\t'+str(npx[i])+'\t'+str(nwv[i])+'\tNN\n') - f.close() - print p - np.polyval(c,w) - refo+=1 - #plot(w,p,'ro') - #plot(nwv,npx,'b') - #show() \ No newline at end of file + f = open('nrorder_'+srefo+'.iwdat','w') + for i in range(len(npx)): + if nwv[i] > w[0] - 100 and nwv[i] < w[-1] + 100: + #print npx[i],nwv[i] + f.write('1\t'+str(npx[i])+'\t'+str(nwv[i])+'\tNN\n') + f.close() + print(p - np.polyval(c,w)) + refo+=1 + #plot(w,p,'ro') + #plot(nwv,npx,'b') + #show() diff --git a/vbt/vbtpipe.py b/vbt/vbtpipe.py index 9c5c7d1..a687507 100644 --- a/vbt/vbtpipe.py +++ b/vbt/vbtpipe.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use("Agg") @@ -14,7 +15,7 @@ baryc_dir= base+'utils/SSEphem/' sys.path.append(baryc_dir) ephemeris='DEc403' - + import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages @@ -103,7 +104,7 @@ force_bkg = False force_P = False force_thar_extract = False -force_thar_wavcal = True +force_thar_wavcal = True force_sci_extract = False force_sci_proc = False # force_RV = False # @@ -140,10 +141,10 @@ models_path = base+'data/COELHO_MODELS/R_40000b/' -print "\n\n\tEchelle Vainu Bappu 2.34m Telescope PIPELINE\n" -print "\tRAW data is in ",dirin -print "\tProducts of reduction will be in",dirout -print '\n' +print("\n\n\tEchelle Vainu Bappu 2.34m Telescope PIPELINE\n") +print("\tRAW data is in ",dirin) +print("\tProducts of reduction will be in",dirout) +print('\n') # file containing the log log = dirout+'night.log' @@ -161,17 +162,17 @@ f = open(log,'r') lines = f.readlines() -print '\tThese are all the images to proccess:' +print('\tThese are all the images to proccess:') for bias in biases: hd = pyfits.getheader(bias) - print '\tbias', hd['IMAGETYP'], hd['IMAGETYP'], hd['DATE-OBS'],bias -print '\n' + print('\tbias', hd['IMAGETYP'], hd['IMAGETYP'], hd['DATE-OBS'],bias) +print('\n') for dark in darks: hd = pyfits.getheader(dark) - print '\tdark', hd['IMAGETYP'], hd['IMAGETYP'], hd['DATE-OBS'],dark -print '\n' + print('\tdark', hd['IMAGETYP'], hd['IMAGETYP'], hd['DATE-OBS'],dark) +print('\n') for line in lines: - print '\t'+line[:-1] + print('\t'+line[:-1]) if stst == 'last': if os.access(dirout+'findstar.txt',os.F_OK): @@ -186,34 +187,34 @@ fst.close() if stblaze == 'same': - print '\n\tThe pipeline will use the image that traces the orders to derive the blaze function ...' + print('\n\tThe pipeline will use the image that traces the orders to derive the blaze function ...') stblaze = stst if ( (os.access(dirout+'MasterFlat.fits',os.F_OK) == False) or\ (os.access(dirout+'trace.pkl',os.F_OK) == False) or \ (os.access(dirout+'MasterBias.fits',os.F_OK) == False) or \ (force_pre_process) ): - print "\tNo previous pre-processing files or found" + print("\tNo previous pre-processing files or found") pre_process = 1 else: - print "\tPre-processing files found, going straight to extraction" + print("\tPre-processing files found, going straight to extraction") pre_process = 0 if (pre_process == 1): - print "\n\tGenerating Master calibration frames..." + print("\n\tGenerating Master calibration frames...") MasterBias, RO_bias, GA_bias = vbtutils.MedianCombine(biases, zero_bo=False, dark_bo=False, flat_bo=False) hdu = pyfits.PrimaryHDU( MasterBias ) if (os.access(dirout+'MasterBias.fits',os.F_OK)): os.remove(dirout+'MasterBias.fits') hdu.writeto(dirout+'MasterBias.fits') - print "\t\t-> Masterbias: done!" + print("\t\t-> Masterbias: done!") MDARKS = [] dark_times = [] if dark_substraction: - + for dark in darks: hd = pyfits.getheader(dark) if len(dark_times) == 0: @@ -222,7 +223,7 @@ if dark_times.count(hd['EXPTIME']) == 0: dark_times.append(hd['EXPTIME']) dark_groups = [] - ndark_times = [] + ndark_times = [] for time in dark_times: group = [] for dark in darks: @@ -244,7 +245,7 @@ hdu.writeto( dirout+'DARK_'+str(int(dark_times[i]))+'s.fits' ) MDARKS.append(dirout+'DARK_'+str(int(dark_times[i]))+'s.fits') i+=1 - print "\t\t-> Masterdarks: done!" + print("\t\t-> Masterdarks: done!") Flat, RO_flat, GA_flat = vbtutils.MedianCombine(flats, zero_bo=True, dark_bo=False, flat_bo=False,zero=dirout+'MasterBias.fits') hdu = pyfits.PrimaryHDU( Flat ) @@ -252,18 +253,18 @@ if (os.access(dirout+'MasterFlat.fits',os.F_OK)): os.remove(dirout+'MasterFlat.fits') hdu.writeto(dirout+'MasterFlat.fits') - print "\t\t-> Masterflat: done!" + print("\t\t-> Masterflat: done!") # Find orders & traces - print "\tTracing echelle orders..." + print("\tTracing echelle orders...") h = pyfits.open(dirin+stst)[0] hth = pyfits.getheader(dirin+stst) d = h.data[0] d = vbtutils.OverscanTrim(d,bsec) d -= MasterBias c_all, nord = GLOBALutils.get_them(d,20,trace_degree,mode=1,nsigmas=3,endat=4100) - print '\t\t'+str(nord)+' orders found...' - + print('\t\t'+str(nord)+' orders found...') + trace_dict = {'c_all':c_all, 'nord':nord, 'DARKS':MDARKS, 'dtimes':dark_times, 'RO_flat':RO_flat, 'GA_flat':GA_flat} pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) ) @@ -285,11 +286,11 @@ h = pyfits.open(dirout+'MasterFlat.fits') Flat = h[0].data -print '\n\tExtraction of Master Flat:' +print('\n\tExtraction of Master Flat:') flat_simple_fits = dirout + 'Flat.spec.simple.fits' flat_fits = dirout + 'Flat.spec.fits' P_fits = dirout + 'P.fits' -if ( os.access(flat_simple_fits,os.F_OK) == False ) or ( os.access(flat_fits,os.F_OK) == False ) or force_flat_extract: +if ( os.access(flat_simple_fits,os.F_OK) == False ) or ( os.access(flat_fits,os.F_OK) == False ) or force_flat_extract: Centers = np.zeros((len(c_all),Flat.shape[1])) for i in range(nord): Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) @@ -318,7 +319,7 @@ flat_simple = pyfits.getdata(flat_simple_fits) flat = pyfits.getdata(flat_fits) -print '\n\tExtraction of ThAr calibration frames:' +print('\n\tExtraction of ThAr calibration frames:') for fsim in ThAr_ref: hth = pyfits.getheader(fsim) @@ -329,8 +330,8 @@ thar_fits_simple = dirout+'ThAr_'+hth['DATE-OBS']+'.spec.simple.fits' - if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): - print "\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting..." + if ( os.access(thar_fits_simple,os.F_OK) == False ) or (force_thar_extract): + print("\t\tNo previous extraction or extraction forced for ThAr file", fsim, "extracting...") Centers = np.zeros((len(c_all),dth.shape[1])) for i in range(nord): Centers[i,:]=scipy.polyval(c_all[i,:],np.arange(len(Centers[i,:]))) @@ -344,10 +345,10 @@ hdu = pyfits.PrimaryHDU( thar_Ss ) hdu.writeto( thar_fits_simple ) else: - print "\t\tThAr file", fsim, "all ready extracted, loading..." + print("\t\tThAr file", fsim, "all ready extracted, loading...") -print "\n\tWavelength solution of ThAr calibration spectra:" +print("\n\tWavelength solution of ThAr calibration spectra:") # compute wavelength calibration files #force_thar_wavcal = True @@ -362,13 +363,13 @@ GAIN = hth['GAIN'] if ( os.access(wavsol_pkl,os.F_OK) == False ) or (force_thar_wavcal): - print " \t\tWorking on ThAr file", thar + print(" \t\tWorking on ThAr file", thar) lines_thar = thar_Ss[:,:] orders_offset, rough_shift = vbtutils.get_thar_offsets(lines_thar,binning=1,pref='order_',suf=sufix) - print 'orders_ofset:',orders_offset - print 'rough_shift:',rough_shift - + print('orders_ofset:',orders_offset) + print('rough_shift:',rough_shift) + orderi = 0 if orders_offset < 0: orderi = - orders_offset @@ -386,7 +387,7 @@ All_Intensities = np.array([]) All_Residuals = np.array([]) All_Sigmas = np.array([]) - + for order in range(orderi,orderf+1): order_s = str(order) if (order < 10): @@ -407,7 +408,7 @@ """ fwhms_lns = sigmas*2.355 inis_lns = pixel_centers - fwhms_lns*0.5 - fins_lns = pixel_centers + fwhms_lns*0.5 + fins_lns = pixel_centers + fwhms_lns*0.5 inis_wvs = GLOBALutils.Cheby_eval(coeffs_pix2wav,inis_lns,float(len(thar_order))) fins_wvs = GLOBALutils.Cheby_eval(coeffs_pix2wav,fins_lns,float(len(thar_order))) fwhms_wvs = inis_wvs - fins_wvs @@ -415,7 +416,7 @@ print "\t\t\tmedian Resolution of order", order, '=', np.around(np.median(resolution2)) #""" - if (order == int(0.5*n_useful)): + if (order == int(0.5*n_useful)): if (use_cheby): Global_ZP = GLOBALutils.Cheby_eval( coeffs_pix2wav, 0.5*len(thar_order), len(thar_order) ) else: @@ -429,7 +430,7 @@ All_Intensities = np.append( All_Intensities, intensities ) All_residuals = np.append( All_Residuals, residuals) All_Sigmas = np.append( All_Sigmas,sigmas) - + p0 = np.zeros( npar_wsol ) p0[0] = int(.5*n_useful) * Global_ZP @@ -467,7 +468,7 @@ if i<10: si = '0'+str(int(i)) else: - si = str(int(i)) + si = str(int(i)) fr = open('wavcals/order_'+si+sufix,'r') tmplines = fr.readlines() f = open('wavcals/order_'+si+sufix,'w') @@ -510,10 +511,10 @@ 'rough_shift':rough_shift} pickle.dump( pdict, open( wavsol_pkl, 'w' ) ) else: - print "\t\tUsing previously computed wavelength solution in file",wavsol_pkl + print("\t\tUsing previously computed wavelength solution in file",wavsol_pkl) pdict = pickle.load(open(wavsol_pkl, 'r')) -print '\n\tProcessing of science images:' +print('\n\tProcessing of science images:') new_list = [] new_list_obnames = [] @@ -536,27 +537,27 @@ for obj in objects: - print '\n' - print "\t--> Working on image: ", obj + print('\n') + print("\t--> Working on image: ", obj) hd = pyfits.getheader(obj) nombre = hd['OBJECT'].replace(' ','') RON, GAIN = hd['RDNOISE'], hd['GAIN'] - print "\t\tObject name:",nombre - - nama = nombre + '_' + hd['DATE-OBS'] + print("\t\tObject name:",nombre) + + nama = nombre + '_' + hd['DATE-OBS'] obj_fits = dirout + nama + '.spec.fits.S' obj_fits_simple = dirout + nama + '.spec.simple.fits.S' - bkg_obj_fits = dirout + 'Bkg_' + nama + '.fits' + bkg_obj_fits = dirout + 'Bkg_' + nama + '.fits' P_fits = dirout + 'P_' + nama + '.fits' if ( os.access(obj_fits,os.F_OK) == False ) or\ ( os.access(obj_fits_simple,os.F_OK) == False ) or\ (force_sci_extract) or ( os.access(P_fits,os.F_OK) == False ): - print "\t\tNo previous extraction or extraction forced for science file", obj, "extracting..." + print("\t\tNo previous extraction or extraction forced for science file", obj, "extracting...") dat = pyfits.getdata(obj)[0] hdt = pyfits.getheader(obj) @@ -599,7 +600,7 @@ hdu.writeto( obj_fits ) obj_Ss = GLOBALutils.simple_extraction(dat,c_alls,ext_aperture,min_extract_col,max_extract_col,npools)[::-1] - + if (os.access(obj_fits_simple,os.F_OK)): os.remove( obj_fits_simple ) hdu = pyfits.PrimaryHDU( obj_Ss ) @@ -610,15 +611,15 @@ ############################################ Final output ###################################################### ################################################################################################################# -print "\n\tBuilding the final output spectra..." +print("\n\tBuilding the final output spectra...") for obj in objects: hd = pyfits.getheader(obj) nombre = hd['OBJECT'].replace(' ','') - nama = nombre + '_' + hd['DATE-OBS'] + nama = nombre + '_' + hd['DATE-OBS'] nf = nama+'_final.fits' - print "\n\t\t-->Building", nf - print dirout+'proc/'+nf + print("\n\t\t-->Building", nf) + print(dirout+'proc/'+nf) if os.access(dirout+'proc/'+nf,os.F_OK) == False or force_sci_proc: # Get observing info from header hd = pyfits.getheader(obj) @@ -659,7 +660,7 @@ RA = ra2 DEC = dec2 else: - print '\t\tUsing the coordinates found in the image header.' + print('\t\tUsing the coordinates found in the image header.') # set info for compute the baricentric correction iers = GLOBALutils.JPLiers( baryc_dir, scmjd-999.0, scmjd+999.0 ) @@ -688,10 +689,10 @@ Sp = jplephem.barycentric_object_track("Sun", int(scmjd), float(scmjd%1), 1, 0.0) res = jplephem.object_doppler("Moon", int(scmjd), scmjd%1, 1, 0.0) lunation,moon_state,moonsep2,moonvel = GLOBALutils.get_lunar_props(ephem,gobs,Mcoo,Mp,Sp,res,RA,DEC) - refvel = bcvel_baryc + moonvel #This is the velocity of the spectrum of the moon with the applied barycentric correction in the direction of the target. + refvel = bcvel_baryc + moonvel #This is the velocity of the spectrum of the moon with the applied barycentric correction in the direction of the target. + + print('\t\t\tBarycentric velocity:',refvel) - print '\t\t\tBarycentric velocity:',refvel - obj_fits = dirout+nama+'.spec.fits.S' obj_fits_simple = dirout+nama+'.spec.simple.fits.S' obj_S = pyfits.getdata(obj_fits) @@ -712,7 +713,7 @@ final = np.zeros( [11, orderf - orderi + 1, np.shape(obj_S)[2]] ) - equis = np.arange( obj_S.shape[2] ) + equis = np.arange( obj_S.shape[2] ) for order in range(orderi,orderf+1): m = order + oro0 @@ -743,7 +744,7 @@ LL = np.where(final[5,order] > 1 + 10. / scipy.signal.medfilt(final[8,order],21))[0] final[5,order,LL] = 1. - final[9,order][L] = final[5,order][L] * (dlambda_dx[L] ** 1) + final[9,order][L] = final[5,order][L] * (dlambda_dx[L] ** 1) final[10,order][L] = final[6,order][L] / (dlambda_dx[L] ** 2) hdu = pyfits.PrimaryHDU( final ) @@ -757,7 +758,7 @@ hdu = GLOBALutils.update_header(hdu,'HIERARCH RA',RA) hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC',DEC) hdu = GLOBALutils.update_header(hdu,'HIERARCH RA BARY',RA) - hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',DEC) + hdu = GLOBALutils.update_header(hdu,'HIERARCH DEC BARY',DEC) hdu = GLOBALutils.update_header(hdu,'HIERARCH EQUINOX',2000) hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LATITUDE',latitude) hdu = GLOBALutils.update_header(hdu,'HIERARCH OBS LONGITUDE',longitude) @@ -797,16 +798,16 @@ spec_moon = np.array(spec_moon) use_moon = np.array(use_moon) -print 'Starting with the Post-processing of the spectra...' +print('Starting with the Post-processing of the spectra...') if not JustExtract: for obj in objects: hdo = pyfits.getheader(obj) nombre = hdo['OBJECT'].replace(' ','') - nama = nombre + '_' + hdo['DATE-OBS'] + nama = nombre + '_' + hdo['DATE-OBS'] fit = dirout + 'proc/' + nama + '_final.fits' - print '\n\t\tWorking on spectrum:', fit + print('\n\t\tWorking on spectrum:', fit) know_moon = False if fsim.split('/')[-1] in spec_moon: I = np.where(obj.split('/')[-1] == spec_moon)[0] @@ -836,7 +837,7 @@ SNR_5130 = np.median(spec[8,tuc][1800:2200] ) if DoClass: - print '\t\tSpectral Analysis:' + print('\t\tSpectral Analysis:') # spectral analysis # First, query SIMBAD with the object name query_success = False @@ -844,14 +845,14 @@ # Now, query SIMBAD by coordinates if above not successful if (not query_success): query_success,sp_type_query = GLOBALutils.simbad_query_coords('12:00:00','00:00:00') - print "\t\t\tSpectral type returned by SIMBAD query:",sp_type_query + print("\t\t\tSpectral type returned by SIMBAD query:",sp_type_query) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH SIMBAD SPTYP', sp_type_query) pars_file = dirout + nombre+'_'+hdo['DATE-OBS']+'_stellar_pars.txt' if os.access(pars_file,os.F_OK) == False or force_stellar_pars: - print "\t\t\tEstimating atmospheric parameters:" + print("\t\t\tEstimating atmospheric parameters:") spec2 = spec.copy() if resolution > 45000: Rx = np.around(1./np.sqrt(1./40000.**2 - 1./resolution**2)) @@ -864,10 +865,10 @@ f.write(line) f.close() else: - print "\t\t\tAtmospheric parameters loaded from file:" + print("\t\t\tAtmospheric parameters loaded from file:") T_eff, logg, Z, vsini, vel0 = np.loadtxt(pars_file,unpack=True) - print "\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0 + print("\t\t\t\tT_eff=",T_eff,"log(g)=",logg,"Z=",Z,"vsin(i)=",vsini,"vel0",vel0) else: T_eff, logg, Z, vsini, vel0 = -999,-999,-999,-999,-999 @@ -878,17 +879,17 @@ Z_epoch = Z vsini_epoch = vsini vel0_epoch = vel0 - + hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VEL0', vel0) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH TEFF', float(T_eff)) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH LOGG', float(logg)) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH Z', Z) hdu[0] = GLOBALutils.update_header(hdu[0],'HIERARCH VSINI', vsini) - print "\t\tRadial Velocity analysis:" + print("\t\tRadial Velocity analysis:") # assign mask sp_type, mask = GLOBALutils.get_mask_reffile(obname,reffile=reffile,base='../data/xc_masks/') - print "\t\t\tWill use",sp_type,"mask for CCF." + print("\t\t\tWill use",sp_type,"mask for CCF.") first_o = 0 for i in range(spec.shape[1]): @@ -903,7 +904,7 @@ break spec1 = spec[:,first_o:last_o+1,:] - + # Read in mask ml, mh, weight = np.loadtxt(mask,unpack=True) ml_v = GLOBALutils.ToVacuum( ml ) @@ -929,8 +930,8 @@ mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide mh_v = av_m + mask_hw_wide - - print '\t\t\tComputing the CCF...' + + print('\t\t\tComputing the CCF...') cond = True while (cond): #first rough correlation to find the minimum @@ -938,7 +939,7 @@ weight, 0, lbary_ltopo, vel_width=300, vel_step=3, start_order=0,\ spec_order=9, iv_order=10, sn_order=8,max_vel_rough=300) xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3, Simple=True, start_order=0, W=W_ccf) - #Normalize the continuum of the CCF robustly with R + #Normalize the continuum of the CCF robustly with R yy = scipy.signal.medfilt(xc_av,11) pred = lowess(yy, vels,frac=0.4,it=10,return_sorted=False) tck1 = scipy.interpolate.splrep(vels,pred,k=1) @@ -946,19 +947,19 @@ xc_av /= pred pred_rough = pred.copy() - vel0_xc = vels[ np.argmin( xc_av ) ] + vel0_xc = vels[ np.argmin( xc_av ) ] rvels, rxc_av, rpred, rxc_av_orig, rvel0_xc = vels.copy(), \ xc_av.copy(), pred.copy(), xc_av_orig.copy(), vel0_xc xc_av_rough = xc_av vels_rough = vels - + vel_width = np.maximum( 20.0, 6*disp ) vels, xc_full, sn, nlines_ccf, W_ccf =\ GLOBALutils.XCor(spec1, ml_v, mh_v, weight, vel0_xc, lbary_ltopo,\ start_order=0, vel_width=vel_width, vel_step=0.1, spec_order=9, \ iv_order=10, sn_order=8,max_vel_rough=300) - + xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3,\ Simple=True, W=W_ccf, start_order=0) @@ -994,11 +995,11 @@ if (not known_sigma): disp = np.floor(p1gau[2]) - if (disp < 3.0): + if (disp < 3.0): disp = 3.0 mask_hw_wide = av_m * disp / (GLOBALutils.Constants.c/1.0e3) ml_v = av_m - mask_hw_wide - mh_v = av_m + mask_hw_wide + mh_v = av_m + mask_hw_wide known_sigma = True else: cond = False @@ -1028,20 +1029,20 @@ else: D = 0.33491 C = 0.00113 - elif sp_type == 'K5': + elif sp_type == 'K5': D = 0.20695 C = 0.00321 - else: + else: D = 0.20695 C = 0.00321 - RV = np.around(p1gau_m[1],4) - BS = np.around(SP,4) + RV = np.around(p1gau_m[1],4) + BS = np.around(SP,4) RVerr2 = 0.400 BSerr = np.around(D / float(np.round(SNR_5130)) + C,4) - print '\t\t\tRV = '+str(RV)+' +- '+str(RVerr2) - print '\t\t\tBS = '+str(BS)+' +- '+str(BSerr) + print('\t\t\tRV = '+str(RV)+' +- '+str(RVerr2)) + print('\t\t\tBS = '+str(BS)+' +- '+str(BSerr)) bjd_out = 2400000.5 + mbjd T_eff_err = 100 @@ -1072,4 +1073,4 @@ Z_epoch, vsini_epoch, XC_min, disp_epoch, TEXP, SNR_5130_R, ccf_pdf) f_res.write(line_out) hdu.close() -f_res.close() \ No newline at end of file +f_res.close() diff --git a/vbt/vbtutils.py b/vbt/vbtutils.py index 42a04e2..186aee2 100644 --- a/vbt/vbtutils.py +++ b/vbt/vbtutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import matplotlib matplotlib.use("Agg") @@ -20,406 +21,406 @@ from pylab import * def get_thar_offsets(lines_thar, order_dir='wavcals/', pref='order_', suf='.iwdat', ior=20,fior=45, delt_or=3, del_width=200.,binning=1): - xcs = [] - for ii in range(ior,fior): - thar_order = lines_thar[ii] - xct = [] - for order in range(ii-delt_or,ii+delt_or): - order_s = str(order) - if (order < 10): - order_s = '0' + order_s - if os.access(order_dir+pref+order_s+suf,os.F_OK): - f = open(order_dir+pref+order_s+suf,'r') - llins = f.readlines() - if len(llins)>5: - pixel_centers_0 = [] - for line in llins: - w = line.split() - nlines = int(w[0]) - for j in range(nlines): - pixel_centers_0.append(float(w[2*j+1])*1./float(binning)) - pixel_centers_0 = np.array(pixel_centers_0).astype('int') - #plot(thar_order) - #plot(pixel_centers_0,thar_order[pixel_centers_0],'ro') - #print order, order_s - #show() - ml = np.array(pixel_centers_0) - 2 - mh = np.array(pixel_centers_0) + 2 - if len(ml)>0: - xc,offs = GLOBALutils.XCorPix( thar_order, ml, mh, del_width=del_width) - else: - xc = np.zeros(len(offs)) - else: - xc = np.array([]) - if len(xct) == 0: - xct = xc.copy() - else: - xct = np.vstack((xct,xc)) - - if len(xcs) == 0: - xcs = xct.copy() - else: - xcs += xct - maxes, maxvels = [],[] - for i in range(xcs.shape[0]): - maxes.append(xcs[i].max()) - maxvels.append(offs[np.argmax(xcs[i])]) - #plot(offs,xcs[i]) - #show() - - maxes,maxvels = np.array(maxes),np.array(maxvels) - orders_offset = -delt_or + np.argmax(maxes) - rough_shift = maxvels[np.argmax(maxes)] - - return orders_offset, rough_shift + xcs = [] + for ii in range(ior,fior): + thar_order = lines_thar[ii] + xct = [] + for order in range(ii-delt_or,ii+delt_or): + order_s = str(order) + if (order < 10): + order_s = '0' + order_s + if os.access(order_dir+pref+order_s+suf,os.F_OK): + f = open(order_dir+pref+order_s+suf,'r') + llins = f.readlines() + if len(llins)>5: + pixel_centers_0 = [] + for line in llins: + w = line.split() + nlines = int(w[0]) + for j in range(nlines): + pixel_centers_0.append(float(w[2*j+1])*1./float(binning)) + pixel_centers_0 = np.array(pixel_centers_0).astype('int') + #plot(thar_order) + #plot(pixel_centers_0,thar_order[pixel_centers_0],'ro') + #print order, order_s + #show() + ml = np.array(pixel_centers_0) - 2 + mh = np.array(pixel_centers_0) + 2 + if len(ml)>0: + xc,offs = GLOBALutils.XCorPix( thar_order, ml, mh, del_width=del_width) + else: + xc = np.zeros(len(offs)) + else: + xc = np.array([]) + if len(xct) == 0: + xct = xc.copy() + else: + xct = np.vstack((xct,xc)) + + if len(xcs) == 0: + xcs = xct.copy() + else: + xcs += xct + maxes, maxvels = [],[] + for i in range(xcs.shape[0]): + maxes.append(xcs[i].max()) + maxvels.append(offs[np.argmax(xcs[i])]) + #plot(offs,xcs[i]) + #show() + + maxes,maxvels = np.array(maxes),np.array(maxvels) + orders_offset = -delt_or + np.argmax(maxes) + rough_shift = maxvels[np.argmax(maxes)] + + return orders_offset, rough_shift def milk_comb(ImgList, darks, zero='Bias.fits'): - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - h = pyfits.open(ImgList[0])[0] - d = h.data - - head = pyfits.getheader(ImgList[0]) - expt = head['EXPTIME'] - - d = OverscanTrim(d,h.header['BIASSEC']) - - Master = pyfits.getdata(zero) - - if len(darks) > 0: - Dark = get_dark(darks,expt) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - - d -= Master - d -= Dark - - factor = 1.25 - if (n < 3): - factor = 1 - - ron1 = h.header['ENOISE'] - gain = h.header['EGAIN'] - - - ronoise = factor * h.header['ENOISE'] / np.sqrt(n) - - if (n == 1): - return d, ron1, gain - - else: - for i in range(n-1): - h = pyfits.open(ImgList[i+1])[0] - head = pyfits.getheader(ImgList[i+1]) - expt = head['EXPTIME'] - if len(darks) > 0: - Dark = get_dark(darks,expt) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - rd = OverscanTrim(h.data,h.header['BIASSEC']) - Master - Dark - d = np.dstack((d,rd/np.median(rd))) - out = np.median(d,axis=2) - return out, ronoise, gain - + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + h = pyfits.open(ImgList[0])[0] + d = h.data + + head = pyfits.getheader(ImgList[0]) + expt = head['EXPTIME'] + + d = OverscanTrim(d,h.header['BIASSEC']) + + Master = pyfits.getdata(zero) + + if len(darks) > 0: + Dark = get_dark(darks,expt) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + + d -= Master + d -= Dark + + factor = 1.25 + if (n < 3): + factor = 1 + + ron1 = h.header['ENOISE'] + gain = h.header['EGAIN'] + + + ronoise = factor * h.header['ENOISE'] / np.sqrt(n) + + if (n == 1): + return d, ron1, gain + + else: + for i in range(n-1): + h = pyfits.open(ImgList[i+1])[0] + head = pyfits.getheader(ImgList[i+1]) + expt = head['EXPTIME'] + if len(darks) > 0: + Dark = get_dark(darks,expt) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + rd = OverscanTrim(h.data,h.header['BIASSEC']) - Master - Dark + d = np.dstack((d,rd/np.median(rd))) + out = np.median(d,axis=2) + return out, ronoise, gain + def FileClassify(path,log): - biases = [] - objects = [] - darks = [] - thars = [] - flat = [] - f = open(log,'w') - archs = glob.glob(path+'*.fits') - if os.access(path+'bad_files.txt',os.F_OK): - ff = open(path + 'bad_files.txt', 'r') - bfiles = ff.readlines() - else: - bfiles = [] - for arch in archs: - use = True - for bf in bfiles: - if arch == path + bf[:-1]: - print 'Dumped file', arch - use = False - break - if use: - h = pyfits.open(arch) - header = pyfits.getheader(arch) - name = header['OBJECT'] - - if True: - if header['IMAGETYP'] == 'object' or header['IMAGETYP'] == 'obj' or header['IMAGETYP'] == 'solar': - expt = header['EXPTIME'] - try: - ra = header['TELRA'] - except: - ra = header['RA'] - try: - dec = header['TELDEC'] - except: - dec = header['DEC'] - ams = header['AIRMASS'] - date = header['DATE-OBS'].split('T')[0] - UT = header['DATE-OBS'].split('T')[1] - line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (name, ra, dec, expt, ams, date, UT, arch) - f.write(line) - objects.append(arch) - elif header['IMAGETYP'] == 'comp': - thars.append(arch) - elif header['IMAGETYP'] == 'zero': - biases.append(arch) - elif header['IMAGETYP'] == 'flat': - flat.append(arch) - elif header['IMAGETYP'] == 'dark': - darks.append(arch) - - h.close() - f.close() - return biases, flat, objects, thars, darks + biases = [] + objects = [] + darks = [] + thars = [] + flat = [] + f = open(log,'w') + archs = glob.glob(path+'*.fits') + if os.access(path+'bad_files.txt',os.F_OK): + ff = open(path + 'bad_files.txt', 'r') + bfiles = ff.readlines() + else: + bfiles = [] + for arch in archs: + use = True + for bf in bfiles: + if arch == path + bf[:-1]: + print('Dumped file', arch) + use = False + break + if use: + h = pyfits.open(arch) + header = pyfits.getheader(arch) + name = header['OBJECT'] + + if True: + if header['IMAGETYP'] == 'object' or header['IMAGETYP'] == 'obj' or header['IMAGETYP'] == 'solar': + expt = header['EXPTIME'] + try: + ra = header['TELRA'] + except: + ra = header['RA'] + try: + dec = header['TELDEC'] + except: + dec = header['DEC'] + ams = header['AIRMASS'] + date = header['DATE-OBS'].split('T')[0] + UT = header['DATE-OBS'].split('T')[1] + line = "%-15s %10s %10s %8.2f %4.2f %8s %8s %s\n" % (name, ra, dec, expt, ams, date, UT, arch) + f.write(line) + objects.append(arch) + elif header['IMAGETYP'] == 'comp': + thars.append(arch) + elif header['IMAGETYP'] == 'zero': + biases.append(arch) + elif header['IMAGETYP'] == 'flat': + flat.append(arch) + elif header['IMAGETYP'] == 'dark': + darks.append(arch) + + h.close() + f.close() + return biases, flat, objects, thars, darks def MedianCombine(ImgList, zero_bo=False, zero='Bias.fits', dark_bo=False, darks=[], flat_bo=False, flat='Flat.fits',bsec=[0,50,4146,4196]): - """ - Median combine a list of images - """ - n = len(ImgList) - if n==0: - raise ValueError("empty list provided!") - - h = pyfits.open(ImgList[0])[0] - d = h.data[0] - d = OverscanTrim(d,bsec) - - if zero_bo: - Master = pyfits.getdata(zero) - else: - Master = np.zeros((d.shape[0],d.shape[1]),float) - - if dark_bo and len(darks)!=0: - hd = pyfits.getheader(ImgList[0]) - time = hd['EXPTIME'] - Dark = get_dark(darks, time) - else: - Dark = np.zeros((d.shape[0],d.shape[1]),float) - - if flat_bo: - Flat = pyfits.getdata(flat) - else: - Flat = np.zeros((d.shape[0],d.shape[1]),float) + 1.0 - - if flat_bo: - d = (d - Master - Dark)/Flat - else: - d = (d - Master - Dark) - - factor = 1.25 - - if (n < 3): - factor = 1 - - ronoise = factor * h.header['RDNOISE'] / np.sqrt(n) - gain = h.header['GAIN'] - - if (n == 1): - return d, ronoise, gain - - else: - for i in range(n-1): - h = pyfits.open(ImgList[i+1])[0] - if flat_bo: - d = np.dstack((d,(OverscanTrim(h.data[0],bsec)-Master-Dark)/Flat)) - else: - - d = np.dstack((d,OverscanTrim(h.data[0],bsec)-Master-Dark)) - - return np.median(d,axis=2), ronoise, gain + """ + Median combine a list of images + """ + n = len(ImgList) + if n==0: + raise ValueError("empty list provided!") + + h = pyfits.open(ImgList[0])[0] + d = h.data[0] + d = OverscanTrim(d,bsec) + + if zero_bo: + Master = pyfits.getdata(zero) + else: + Master = np.zeros((d.shape[0],d.shape[1]),float) + + if dark_bo and len(darks)!=0: + hd = pyfits.getheader(ImgList[0]) + time = hd['EXPTIME'] + Dark = get_dark(darks, time) + else: + Dark = np.zeros((d.shape[0],d.shape[1]),float) + + if flat_bo: + Flat = pyfits.getdata(flat) + else: + Flat = np.zeros((d.shape[0],d.shape[1]),float) + 1.0 + + if flat_bo: + d = (d - Master - Dark)/Flat + else: + d = (d - Master - Dark) + + factor = 1.25 + + if (n < 3): + factor = 1 + + ronoise = factor * h.header['RDNOISE'] / np.sqrt(n) + gain = h.header['GAIN'] + + if (n == 1): + return d, ronoise, gain + + else: + for i in range(n-1): + h = pyfits.open(ImgList[i+1])[0] + if flat_bo: + d = np.dstack((d,(OverscanTrim(h.data[0],bsec)-Master-Dark)/Flat)) + else: + + d = np.dstack((d,OverscanTrim(h.data[0],bsec)-Master-Dark)) + + return np.median(d,axis=2), ronoise, gain def OverscanTrim(d,bsec,bin=1.): - """ - Overscan correct and Trim a refurbished DuPont image - """ + """ + Overscan correct and Trim a refurbished DuPont image + """ - t1 = d[:,bsec[0]:bsec[1]] - t2 = d[:,bsec[2]:bsec[3]] - - nd = d[:,bsec[1]:bsec[2]].copy() + t1 = d[:,bsec[0]:bsec[1]] + t2 = d[:,bsec[2]:bsec[3]] - overscan1 = np.median(np.dstack((t1,t2))) - newdata = nd - overscan1 + nd = d[:,bsec[1]:bsec[2]].copy() - return newdata + overscan1 = np.median(np.dstack((t1,t2))) + newdata = nd - overscan1 + + return newdata def get_dark(darks,t): - exact = 0 - dts = [] - for dark in darks: - hd = pyfits.getheader(dark) - dt = hd['EXPTIME'] - dts.append(dt) - if dt == t: - DARK = pyfits.getdata(dark) - exact = 1 - - dts = np.array(dts) - if exact == 0: - if t < dts.min(): - I = np.where( dts == dts.min() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - elif t > dts.max(): - I = np.where( dts == dts.max() )[0] - DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] - else: - tmin = dts.min() - tmax = dts.max() - I = np.where( dts == dts.min() )[0] - Dmin = pyfits.getdata(darks[I[0]]) - Dminname=darks[I[0]] - I = np.where( dts == dts.max() )[0] - Dmax = pyfits.getdata(darks[I[0]]) - Dmaxname = darks[I[0]] - - i = 0 - while i < len(dts): - if dts[i] < t and dts[i] > tmin: - tmin = dts[i] - Dminname = darks[i] - Dmin = pyfits.getdata(darks[i]) - elif dts[i] > t and dts[i] < tmax: - tmax = dts[i] - Dmaxname = darks[i] - Dmax = pyfits.getdata(darks[i]) - i+=1 - - num = Dmax - Dmin - den = tmax-tmin - m = num/den - n = Dmax - m*tmax - DARK = m*t+n - - return DARK + exact = 0 + dts = [] + for dark in darks: + hd = pyfits.getheader(dark) + dt = hd['EXPTIME'] + dts.append(dt) + if dt == t: + DARK = pyfits.getdata(dark) + exact = 1 + + dts = np.array(dts) + if exact == 0: + if t < dts.min(): + I = np.where( dts == dts.min() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + elif t > dts.max(): + I = np.where( dts == dts.max() )[0] + DARK = pyfits.getdata(darks[I[0]])*t/dts[I[0]] + else: + tmin = dts.min() + tmax = dts.max() + I = np.where( dts == dts.min() )[0] + Dmin = pyfits.getdata(darks[I[0]]) + Dminname=darks[I[0]] + I = np.where( dts == dts.max() )[0] + Dmax = pyfits.getdata(darks[I[0]]) + Dmaxname = darks[I[0]] + + i = 0 + while i < len(dts): + if dts[i] < t and dts[i] > tmin: + tmin = dts[i] + Dminname = darks[i] + Dmin = pyfits.getdata(darks[i]) + elif dts[i] > t and dts[i] < tmax: + tmax = dts[i] + Dmaxname = darks[i] + Dmax = pyfits.getdata(darks[i]) + i+=1 + + num = Dmax - Dmin + den = tmax-tmin + m = num/den + n = Dmax - m*tmax + DARK = m*t+n + + return DARK def get_blaze(LL,FF, low=1.0, hi=3.0, n = 6): - NF = FF.copy() - for j in range(LL.shape[0]): - L = LL[j] - F = FF[j] - ejex = np.arange(len(F)) - F[:150] = 0.0 - F[-150:] = 0.0 - Z = np.where(F!=0)[0] - F = scipy.signal.medfilt(F[Z],31) - ejexx = ejex.copy() - ejex = ejex[Z] - L = L[Z] - I = np.where((L>5870) & (L<5890))[0] - if len(I)>0: - W = np.where(L<5870)[0] - R = np.where(L>5890)[0] - ejetemp = np.hstack((ejex[W],ejex[R])) - Ftemp = np.hstack((F[W],F[R])) - coefs = np.polyfit(ejetemp,Ftemp,n) - fit = np.polyval(coefs,ejetemp) - - else: - ejetemp=ejex - Ftemp=F - coefs = np.polyfit(ejex,F,n) - fit = np.polyval(coefs,ejex) - i = 0 - while i < 30: - res = Ftemp - fit - IP = np.where((res>=0) & (Ftemp!=0.0))[0] - IN = np.where((res<0) & (Ftemp!=0.0))[0] - devp = np.mean(res[IP]) - devn = np.mean(res[IN]) - I = np.where((res > -low*abs(devn)) & (res < hi*abs(devp)) & (Ftemp!=0))[0] - coefs = np.polyfit(ejetemp[I],Ftemp[I],n) - fit = np.polyval(coefs,ejetemp) - i+=1 - fit = np.polyval(coefs,ejexx) - - NF[j]=fit - NNF = NF.copy() - for j in range(LL.shape[0]): - - L = LL[j] - I = np.where((L>6520) & (L<6600))[0] - if len(I)>0: - - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j+1 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) - - - I = np.where((L>4870) & (L<4880))[0] - if len(I)>0: - - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - elif j+1 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - else: - for i in range(len(L)): - vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) - NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) - - I = np.where((L>4320) & (L<4325))[0] - if len(I)>0: - if j+2 < LL.shape[0]: - for i in range(len(L)): - vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) - tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) - NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) - return NNF + NF = FF.copy() + for j in range(LL.shape[0]): + L = LL[j] + F = FF[j] + ejex = np.arange(len(F)) + F[:150] = 0.0 + F[-150:] = 0.0 + Z = np.where(F!=0)[0] + F = scipy.signal.medfilt(F[Z],31) + ejexx = ejex.copy() + ejex = ejex[Z] + L = L[Z] + I = np.where((L>5870) & (L<5890))[0] + if len(I)>0: + W = np.where(L<5870)[0] + R = np.where(L>5890)[0] + ejetemp = np.hstack((ejex[W],ejex[R])) + Ftemp = np.hstack((F[W],F[R])) + coefs = np.polyfit(ejetemp,Ftemp,n) + fit = np.polyval(coefs,ejetemp) + + else: + ejetemp=ejex + Ftemp=F + coefs = np.polyfit(ejex,F,n) + fit = np.polyval(coefs,ejex) + i = 0 + while i < 30: + res = Ftemp - fit + IP = np.where((res>=0) & (Ftemp!=0.0))[0] + IN = np.where((res<0) & (Ftemp!=0.0))[0] + devp = np.mean(res[IP]) + devn = np.mean(res[IN]) + I = np.where((res > -low*abs(devn)) & (res < hi*abs(devp)) & (Ftemp!=0))[0] + coefs = np.polyfit(ejetemp[I],Ftemp[I],n) + fit = np.polyval(coefs,ejetemp) + i+=1 + fit = np.polyval(coefs,ejexx) + + NF[j]=fit + NNF = NF.copy() + for j in range(LL.shape[0]): + + L = LL[j] + I = np.where((L>6520) & (L<6600))[0] + if len(I)>0: + + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j+1 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) + + + I = np.where((L>4870) & (L<4880))[0] + if len(I)>0: + + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + elif j+1 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + else: + for i in range(len(L)): + vec = np.array([NF[j-3,i],NF[j-2,i],NF[j-1,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,2.0]),vec,k=1) + NNF[j,i] = scipy.interpolate.splev(3.0,tck,der=0) + + I = np.where((L>4320) & (L<4325))[0] + if len(I)>0: + if j+2 < LL.shape[0]: + for i in range(len(L)): + vec = np.array([NF[j-2,i],NF[j-1,i],NF[j+1,i],NF[j+2,i]]) + tck = scipy.interpolate.splrep(np.array([0.0,1.0,3.0,4.0]),vec,k=2) + NNF[j,i] = scipy.interpolate.splev(2.0,tck,der=0) + return NNF def get_close(tht,rat,dect,fits): - t0 = 1000000. - close = fits[0] - for fit in fits: - #print close - hd = pyfits.getheader(fit) - sct,mjd0 = mjd_fromheader(hd) - expt = hd['EXPTIME']/(3600.*24.) - dec = hd['DEC-D'] - ra = hd['RA-D'] - if abs(dec - dect)<0.05 and abs(ra - rat)<0.05: - #print sct+expt,tht - if abs(sct+expt-tht) < t0: - t0 = abs(sct+expt-tht) - close = fit - return close - - + t0 = 1000000. + close = fits[0] + for fit in fits: + #print close + hd = pyfits.getheader(fit) + sct,mjd0 = mjd_fromheader(hd) + expt = hd['EXPTIME']/(3600.*24.) + dec = hd['DEC-D'] + ra = hd['RA-D'] + if abs(dec - dect)<0.05 and abs(ra - rat)<0.05: + #print sct+expt,tht + if abs(sct+expt-tht) < t0: + t0 = abs(sct+expt-tht) + close = fit + return close + + def b_col(d): - d[:,746] = 0.5*(d[:,745]+d[:,748]) - d[:,747] = 0.5*(d[:,745]+d[:,748]) - return d + d[:,746] = 0.5*(d[:,745]+d[:,748]) + d[:,747] = 0.5*(d[:,745]+d[:,748]) + return d def mjd_fromheader(h): """ return modified Julian date from header - """ + """ datetu = h['DATE-OBS'].split('T')[0] timetu = h['DATE-OBS'].split('T')[1] mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:])) @@ -435,4 +436,4 @@ def mjd_fromheader(h): mjd = mjd_start + (fraction * texp) / secinday - return mjd, mjd0 \ No newline at end of file + return mjd, mjd0