def plotSpecVsWLen(): wLen = getWavelengthArr(specFile, 0) spec = getImageData(specFile, 0) # wLen = range(len(spec)) for i in range(len(wLen)): print('wLen = ', wLen[i], ', spec = ', spec[i]) fig, ax = plt.subplots() fig.set_size_inches(18.5, 10.5) ax.plot(wLen, spec) #plt.show() ax.set_xlabel('Wavelength [$\mathrm{\AA}$]') ax.set_ylabel( '$\mathrm{F}_\lambda$ [$\mathrm{erg}$ $\mathrm{s^{-1}} \mathrm{cm^{-2}} \mathrm{\AA^{-1}}]$' ) #ax.set_xlim([0,2000]) yLim = 1e6 #ax.get_ylim()[1] ax.set_ylim([0, yLim * 1.3]) lineid_plot.plot_line_ids(wLen, spec, linePos, lineLabel, ax=ax, arrow_tip=yLim * 1.1, box_loc=yLim * 1.27) plt.title = specFile[specFile.rfind('/') + 1:] plt.savefig(specFile[:specFile.rfind('.')] + '.pdf', bbox_inches='tight') plt.show()
def plotAug2013(): spec = getImageData(aug2013Arc, 0) wLen = getWavelengthArr(aug2013Arc, 0) fig, ax = plt.subplots() fig.set_size_inches(18.5, 10.5) ax.plot(wLen, spec) ax.set_xlabel('Wavelength [$\mathrm{\AA}$]') ax.set_ylabel( '$\mathrm{F}_\lambda$ [$\mathrm{erg}$ $\mathrm{s^{-1}} \mathrm{cm^{-2}} \mathrm{\AA^{-1}}]$' ) #ax.set_xlim([0,2000]) yLim = ax.get_ylim()[1] ax.set_ylim([0, yLim * 1.4]) lineid_plot.plot_line_ids(wLen, spec, [float(l) for l in lineLabel], lineLabel, ax=ax, arrow_tip=yLim * 1.1, box_loc=yLim * 1.27) plt.savefig(aug2013Arc[:aug2013Arc.rfind('.')] + '.pdf', bbox_inches='tight') plt.show()
line = line.strip().split() print('line = ', line) linePosRef.append(float(line[0])) lineLabelRef.append(line[1]) with open(lineListFile, 'r') as f: lines = f.readlines() linePos = [] lineLabel = [] for line in lines: line = line.strip().split() print('line = ', line) linePos.append(float(line[0])) lineLabel.append(line[1]) refSpec = getImageData(refSpecFile, 0) def plotRefSpec(lam, flux, title='refArc_spupnic_may2007_d.fits'): fig, ax = plt.subplots() fig.set_size_inches(18.5, 10.5) #ax.plot(wLenArr[0:len(wLenArr)-1],spec) ax.plot(lam, flux) ax.set_xlabel('Wavelength [$\mathrm{\AA}$]') ax.set_ylabel( '$\mathrm{F}_\lambda$ [$\mathrm{erg}$ $\mathrm{s^{-1}} \mathrm{cm^{-2}} \mathrm{\AA^{-1}}]$' ) #ax.set_xlim([0,2000]) yLim = ax.get_ylim()[1] ax.set_ylim([0, yLim * 1.4])
import numpy as np from drUtils import xCor, getImageData, xCorFindMinimum inputLineList = '/Users/azuri/stella/referenceFiles/spupnic/saao_refspec_gr7_angle16_3_lines_identified_good.dat' outputLineList = '/Users/azuri/stella/referenceFiles/spupnic/saao_refspec_gr7_angle16_3_may2020_lines_identified_good.dat' oldArcSpec = '/Users/azuri/spectra/saao/saao_sep2019/20190907/refArc_spupnic_gr7_16_30_otzxfifEc.fits' newArcSpec = '/Users/azuri/spectra/saao/saao_may2020/20200519/refARC_spupnic_grat7_16_30_otzxf_may2020iEc.fits' oldSpec = getImageData(oldArcSpec, 0) oldSpec /= np.mean(oldSpec) newSpec = getImageData(newArcSpec, 0) newSpec /= np.mean(newSpec) oldX = np.arange(0, len(oldSpec), 1) newX = np.arange(0, len(newSpec), 1) static = [oldX, oldSpec] moving = [newX[40:newX.shape[0] - 40], newSpec[40:newX.shape[0] - 40]] print('static[0][1] = ', static[0][1]) print('moving[0][1] = ', moving[0][1]) result = xCor(static, moving) print('result = ', result) print('result[0] = ', result[0]) print('result[1] = ', result[1]) minPos = xCorFindMinimum(result[0], result[1]) print('minPos = ', minPos)
def getSigmaVsWLen(spectrumName, nPix=20, show=False): wLen = getWavelengthArr(spectrumName, 0) print(spectrumName, ': wLen = ', wLen) arrayLength = wLen.shape[0] spec = getImageData(spectrumName, 0) if show: plt.plot(wLen, spec) plt.show() sigmas = np.zeros(arrayLength) for i in range(arrayLength): if i < int(nPix / 2.): sigmas[i] = np.std(spec[:nPix]) elif i > arrayLength - int(nPix / 2.): sigmas[i] = np.std(spec[:-nPix]) else: sigmas[i] = np.std(spec[i - int(nPix / 2.):i + int(nPix / 2.) + 1]) sigmaSpecFileName = os.path.join( spectrumName[:spectrumName.rfind('/')], 'test', 'sigma' + spectrumName[spectrumName.rfind('/') + 1:]) sigmaFitSpecFileName = os.path.join( spectrumName[:spectrumName.rfind('/')], 'test', 'sigmaFit' + spectrumName[spectrumName.rfind('/') + 1:]) writeFits1D( sigmas, sigmaSpecFileName, wavelength=None, header=spectrumName, CRVAL1=getHeaderValue(spectrumName, 'CRVAL1'), CRPIX1=getHeaderValue(spectrumName, 'CRPIX1'), CDELT1=getHeaderValue(spectrumName, 'CDELT1'), ) fittingFunction = np.polynomial.legendre.legfit evalFunction = np.polynomial.legendre.legval order = 4 nIterReject = 2 nIterFit = 2 lowReject = 3. highReject = 1. useMean = False continuum(sigmaSpecFileName, sigmaFitSpecFileName, fittingFunction, evalFunction, order, nIterReject, nIterFit, lowReject, highReject, type='fit', adjustSigLevels=False, useMean=useMean, display=show) fit = getImageData(sigmaFitSpecFileName, 0) if show: plt.plot(wLen, spec) plt.plot(wLen, sigmas) plt.plot(wLen, fit) plt.show() return [wLen, fit]
def calculateErrors(spectrumFileName, idPNMain, csvLinesFileName, show=False): print('spectrumFileName = <' + spectrumFileName + '>') wLen, sigmaFit = getSigmaVsWLen(spectrumFileName, show=False) print('len(wLen) = ', len(wLen), ', len(sigmaFit) = ', len(sigmaFit)) flux = getImageData(spectrumFileName, 0) print('len(flux) = ', len(flux)) csvLines = csvFree.readCSVFile(csvLinesFileName, '\t', False) csvVRad = csvFree.readCSVFile( os.path.join(imPath[:imPath.rfind('/')], 'vrad.csv')) print('csvVRad.header = ', csvVRad.header) filenames = csvVRad.getData('fileName') print('filenames = ', filenames) vradPos = csvVRad.find('fileName', spectrumFileName[spectrumFileName.rfind('/') + 1:], 0)[0] if vradPos < 0: print('error: did not find spectrumFileName <' + spectrumFileName + '>') #STOP else: vrad = float(csvVRad.getData('vrad', vradPos)) print('vrad = ', type(vrad), ': ', vrad) wLen = applyVRadCorrection(wLen, vrad) print('vradPos = ', vradPos) header = csvLines.header keys = list(linesOfInterest.keys()) for i in range(csvLines.size()): if idPNMain == csvLines.getData('NAME', i): for iLine in range(len(linesOfInterest)): area = float(csvLines.getData(keys[iLine], i)) print('key = ', keys[iLine], ': area = ', area) if area > 0.: x0 = linesOfInterest[keys[iLine]] x = wLen[np.where(np.abs(wLen - x0) < 20.)[0]] thisFlux = flux[np.where(np.abs(wLen - x0) < 3.)[0]] maxFlux = np.max(thisFlux) sigma = area / (maxFlux * 2.13 * np.sqrt(2. * np.log(2.))) print('x = ', x0, ', a = ', maxFlux, ', sigma = ', sigma) thisFlux = flux[np.where(np.abs(wLen - x0) < 20.)[0]] thisSDev = sigmaFit[np.where( np.abs(wLen - x0) < 20.)[0]] gaussFit = gauss(x, maxFlux, x0, sigma) if show: plt.plot(x, thisFlux, label='flux') plt.plot(x, thisSDev, label='sigma') plt.plot(x, gaussFit, label='fit') plt.legend() plt.show() newArea = getAreaGauss(x, thisFlux, maxFlux, x0, sigma, addOnBothSidesOfX=0., show=False, save=None) print('old area = ', area, ', newly fitted area = ', newArea) if show: plt.plot(x, gaussFit, label='fit') plt.plot(x, thisFlux, label='flux') newAreas = [] for iRun in range(100): thisFluxWithErr = np.zeros(x.shape, dtype='float32') for thisFluxPos in range(x.shape[0]): thisFluxWithErr[thisFluxPos] = gaussFit[ thisFluxPos] + np.random.normal( 0., np.abs(thisSDev[thisFluxPos])) if show: plt.plot(x, thisFluxWithErr, label='%d' % (iRun)) try: newAreas.append( getAreaGauss(x, thisFluxWithErr, maxFlux, x0, sigma, addOnBothSidesOfX=0., show=False, save=None)[0]) except Exception as e: plt.plot(x, thisFlux, label='original') plt.plot(x, thisFluxWithErr, label='with errors') plt.show() newAreas.append(area) STOP if show: plt.legend() plt.show() newAreas = np.array(newAreas) print('newAreas = ', len(newAreas), ': ', newAreas) if show: plt.hist(newAreas) plt.show() sDev = np.std(newAreas) print('sDev = ', sDev) csvLines.setData(keys[iLine] + 'e', i, '%.3E' % (sDev)) csvFree.writeCSVFile(csvLines, csvLinesFileName, '\t')
import astropy.io.fits as pyfits import lineid_plot import matplotlib matplotlib.use('tkagg') import matplotlib.pyplot as plt import numpy as np from drUtils import getWavelengthArr, getImageData, readMyPNLineList import csvFree, csvData csFileName = '/Users/azuri/daten/uni/HKU/IPHAS-GTC/hash_PNMain_tbCSCoords_240222.csv' fitsFileName = '/Users/azuri/daten/uni/HKU/IPHAS-GTC/IPHASGTC_Ju1_GT110616_id4408.fits' #IPHASGTC_KK26_GT090316_id4393.fits' lineListName = '/Users/azuri/entwicklung/python/data_reduction/pnLineList_Ju1.dat' #KK26.dat' wLen = getWavelengthArr(fitsFileName, 0) spec = getImageData(fitsFileName, 0) lineWave, lineLabel = readMyPNLineList(lineListName) fig, ax = plt.subplots() ax.plot(wLen, spec) ax.set_xlabel('Wavelength [$\mathrm{\AA}$]') ax.set_ylabel( '$\mathrm{F}_\lambda$ [$\mathrm{erg}$ $\mathrm{s^{-1}} \mathrm{cm^{-2}} \mathrm{\AA^{-1}}]$' ) ax.set_xlim([4000, 7200]) ax.set_ylim([-0.1e-15, 3.1e-15]) lineid_plot.plot_line_ids(wLen, spec, lineWave,