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..."
#print '\t\ty shift = ', pshift, 'pxs' data = np.dstack((h[1].data, h[2].data)) - MasterBias centers1 = np.zeros((len(c_all1), data.shape[0])) centers2 = np.zeros((len(c_all2), data.shape[0])) for i in range(len(c_all1)): centers1[i] = np.polyval(c_all1[i], np.arange(data.shape[0])) 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...' 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) bac2 = GLOBALutils.get_scat(data[:, :, 1].T, centers2, span=sky_aperture) bac = np.dstack((bac1.T, bac2.T)) 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) data = data - bac if (os.access(sky_fits, os.F_OK) == False) or force_bac:
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' 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])) 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) else: bac = pyfits.getdata(bac_fits) data = data - bac data = data / nflat.T ra = h[0].header['RA'].split(':') ra = float(ra[0]) + float(ra[1]) / 60. + float(ra[2]) / 3600. ra = 360. * ra / 24. dec = h[0].header['DEC'].split(':') dec = float(dec[0]) + float(dec[1]) / 60. + float(dec[2]) / 3600.
print('\n\tExtraction of Flat calibration frames:') P_fits = dirout + 'P.fits' S_flat_fits = dirout +'flat.fits' S_flat = np.zeros((nord, 3, Flat.shape[1]) ) 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...") 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,:]))) bac = GLOBALutils.get_scat(Flat,Centers,span=7) fl = Flat - bac #plot(fl[:,1000]) #plot(np.around(Centers[:,1000]).astype('int'),fl[np.around(Centers[:,1000].astype('int')),1000],'ro') #show() #print gfd bacfile = dirout + 'BAC_FLAT.fits' if (os.access(bacfile,os.F_OK)): os.remove( bacfile ) hdbac = pyfits.PrimaryHDU( bac ) hdbac.writeto(bacfile) 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,\
h = pyfits.open(dirin + stst)[0] hth = pyfits.getheader(dirin + stst) d = h.data d = pfsutils.OverscanTrim(d, bias_section, over_section) d -= MasterBias d /= NorFlat Centers = np.zeros((len(c_all), d.shape[1])) for i in range(nord): Centers[i, :] = scipy.polyval(c_all[i, :], np.arange(len(Centers[i, :]))) force_bkg = True bkg_obj_fits = dirout + 'BKG_' + h.header['UT-DATE'] + '_' + h.header[ 'UT-TIME'] + '.' + h.header['OBJECT'] + '.fits' if (os.access(bkg_obj_fits, os.F_OK) == False or force_bkg): bkg = GLOBALutils.get_scat(d, Centers, span=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) d -= bkg RO, GA = hth['ENOISE'], hth['EGAIN'] P = GLOBALutils.obtain_P(d,c_all,ext_aperture,RO,\ GA,NSigma_Marsh, S_Marsh, \ N_Marsh, Marsh_alg, min_extract_col,\ max_extract_col, npools)
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)
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
MDARKS = trace_dict['DARKS'] dark_times = trace_dict['dtimes'] h = pyfits.open(dirout+'MasterFlat.fits') Flat = h[0].data 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: 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,:]))) bkg = GLOBALutils.get_scat(Flat,Centers,span=15) Flat -= bkg flat_simple = GLOBALutils.simple_extraction(Flat,c_all,ext_aperture,min_extract_col,max_extract_col,npools) P = GLOBALutils.obtain_P(Flat,c_all,ext_aperture,RO_flat,GA_flat,NSigma_Marsh,S_Marsh,N_Marsh,Marsh_alg,min_extract_col,max_extract_col,npools) flat = GLOBALutils.optimal_extraction(Flat,P,c_all,ext_aperture,RO_flat,GA_flat,S_Marsh,NCosmic_Marsh,min_extract_col,max_extract_col,npools) flat_simple = flat_simple[::-1] flat = flat[::-1] if (os.access(flat_simple_fits,os.F_OK)): os.remove( flat_simple_fits ) hdu = pyfits.PrimaryHDU( flat_simple ) hdu.writeto( flat_simple_fits ) if (os.access(flat_fits,os.F_OK)): os.remove( flat_fits ) hdu = pyfits.PrimaryHDU( flat ) hdu.writeto( flat_fits )
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()