def loadSpectra(self): self.wave = [] self.flux = [] for i, good in zip(range(self.nSpectra), self.inWlRange): if good: data = pyfits.getdata(self.datafile, ext=i+1) if self.resolvingPower != None: wave, flux = SpectralTools.resample(data.field('Wavelength'), data.field('Stokes_I'), self.resolvingPower) self.wave.append(wave) self.flux.append(flux) else: self.wave.append(data.field('Wavelength')) self.flux.append(data.field('Stokes_I')) else: self.wave.append([]) self.flux.append([]) #self.wave, self.flux = SpectralTools.resample(data[0], data[1], # self.resolvingPower) self.wave = numpy.array(self.wave) self.flux = numpy.array(self.flux)
def calculateCM(SynthObj, nominalWave, nominalSpectrum): solarWl = SynthObj.solarSpectrum.wave solarFl = SynthObj.solarSpectrum.flux resolution = 45000.0 nLines = SynthObj.lineList.numLines nModes = 2 * nLines + 3 nFilt = nLines IM = numpy.zeros((nModes, len(solarWl))) stroke = numpy.ones(nModes) factor = numpy.ones(nModes) stroke[-1] = 0.005 # Continuum Shift stroke[-2] = 0.1 # WL Shift stroke[-3] = 0.01 # Smoothing for i in range(nLines): SynthObj.lineList.writeLineLists(i) wave, flux = SynthObj.run() stroke[i] = SynthObj.lineList.getGf(i) / 5.0 stroke[i + nLines] = 0.1 """ while ((factor[i] < 0.9) | (factor[i] > 1.1)): stroke[i] *= factor[i] SynthObj.lineList.perturbGf(i, stroke[i]) SynthObj.lineList.writeLineLists(i,partial=True) wave, plus = SynthObj.run() wave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbGf(i, -2.0*stroke[i]) SynthObj.lineList.writeLineLists(i,partial=True) wave, minus = SynthObj.run() wave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbGf(i, stroke[i]) factor[i] = numpy.abs(target[i]/(numpy.min(plus)-numpy.min(minus))) print factor[i] raw_input() #""" for i in range(nLines): # log gf SynthObj.lineList.perturbGf(i, stroke[i]) SynthObj.lineList.writeLineLists(i) wave, plus = SynthObj.run() newWave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbGf(i, -2.0 * stroke[i]) SynthObj.lineList.writeLineLists(i) wave, minus = SynthObj.run() newWave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbGf(i, stroke[i]) IM[i, :] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus - minus) / (2.0 * stroke[i]), pad=0.0) factor[i] = numpy.max(numpy.abs(IM[i, :])) IM[i, :] /= factor[i] # damping SynthObj.lineList.perturbVdW(i, stroke[i + nLines]) SynthObj.lineList.writeLineLists(i) wave, plus = SynthObj.run() newWave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbVdW(i, -2.0 * stroke[i + nLines]) SynthObj.lineList.writeLineLists(i) wave, minus = SynthObj.run() newWave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbVdW(i, stroke[i + nLines]) IM[i + nLines, :] = SpectralTools.interpolate_spectrum( newWave, solarWl, (plus - minus) / (2.0 * stroke[i + nLines]), pad=0.0) factor[i + nLines] = numpy.max(numpy.abs(IM[i + nLines, :])) IM[i + nLines, :] /= factor[i + nLines] #Continuum Level plus = nominalSpectrum + stroke[-1] newWave, plus = SpectralTools.resample(nominalWave, plus, resolution) minus = nominalSpectrum - stroke[-1] newWave, minus = SpectralTools.resample(nominalWave, minus, resolution) IM[-1, :] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus - minus) / (2.0 * stroke[-1]), pad=0.0) factor[-1] = numpy.max(numpy.abs(IM[-1, :])) IM[-1, :] /= factor[-1] #Wavelength Shift plus = SpectralTools.interpolate_spectrum(wave, wave - stroke[-2], nominalSpectrum, pad=True) newWave, plus = SpectralTools.resample(wave, plus, resolution) minus = SpectralTools.interpolate_spectrum(wave, wave + stroke[-2], nominalSpectrum, pad=True) newWave, minus = SpectralTools.resample(wave, minus, resolution) IM[-2, :] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus - minus) / (2.0 * stroke[-2]), pad=0.0) factor[-2] = numpy.max(numpy.abs(IM[-2, :])) IM[-2, :] /= factor[-2] #""" #Instrumental Smoothing wavePlus, plus = SpectralTools.resample(wave, nominalSpectrum, resolution * (1.0 + stroke[-3])) waveMinus, minus = SpectralTools.resample(wave, nominalSpectrum, resolution * (1.0 - stroke[-3])) diffx, diffy = SpectralTools.diff_spectra(wavePlus, plus, waveMinus, minus, pad=True) IM[-3, :] = SpectralTools.interpolate_spectrum(diffx, solarWl, diffy / (2.0 * stroke[-3]), pad=0.0) factor[-3] = numpy.max(numpy.abs(IM[-3, :])) IM[-3, :] /= factor[-3] #""" hdu = pyfits.PrimaryHDU(IM) hdu.writeto("InteractionMatrix.fits", clobber=True) #nFilt = Synth.lineList.numLines-2 dims = IM.shape U, S, V = scipy.linalg.svd(IM) D = 1.0 / S #D = 1.0/(S[0:-nFilt]) #S[-nFilt:] = 0.0 newS = numpy.zeros((dims[0], dims[1])) I = [i for i in range(dims[1])] for i in range(len(D)): newS[i][i] = D[i] S = newS.copy() CM = numpy.array(scipy.matrix(V.T.dot(S.T.dot(U.T)), dtype=numpy.float32)).T fig = pyplot.figure(0) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax.plot(S) hdu = pyfits.PrimaryHDU(CM) hdu.writeto('CommandMatrix.fits', clobber=True) hdu = pyfits.PrimaryHDU(factor) hdu.writeto('scalingfactor.fits', clobber=True)
diffplot = False Synth = MoogTools.Moog(configFile) Synth.lineList.writeLineLists() Synth.parameterFile.writeParFile() solarWave = Synth.solarSpectrum.wave + 0.1 solarFlux = Synth.solarSpectrum.flux + 0.001 + numpy.random.randn( len(solarWave)) * 0.001 continuum = 0.0 wlOffset = 0.0 resolution = 45000 nominalWavelength, nominalSpectrum = Synth.run() wave, spectrum = SpectralTools.resample(nominalWavelength, nominalSpectrum, resolution) if True: calculateCM(Synth, nominalWavelength, nominalSpectrum) CM = pyfits.getdata("CommandMatrix.fits") factor = pyfits.getdata("scalingfactor.fits") f1 = pyplot.figure(0) f1.clear() ax1 = f1.add_axes([0.1, 0.1, 0.8, 0.8]) cb = ax1.matshow(CM, aspect='auto') blah = pyplot.colorbar(cb) f1.show()
edges = (plus != 0) & (minus != 0) IM[-2, edges] = (plus[edges] - minus[edges]) / (0.2) nFilt = Synth.lineList.numLines - 10 dims = IM.shape U, S, V = scipy.linalg.svd(IM) D = 1.0 / (S[0:-nFilt]) S[-nFilt:] = 0.0 newS = numpy.zeros((dims[0], dims[1])) I = [i for i in range(dims[1])] for i in range(len(D)): newS[i][i] = D[i] S = newS.copy() CM = numpy.array(scipy.matrix(V.T.dot(S.T.dot(U.T)), dtype=numpy.float32)).T hdu = pyfits.PrimaryHDU(CM) hdu.writeto('CommandMatrix_new.fits', clobber=True) configFile = 'CMTeacher.cfg' Synth = MoogTools.Moog(configFile) Synth.lineList.writeLineLists() Synth.parameterFile.writeParFile() wavelengths, nominalSpectrum = Synth.run() newWave, newFlux = SpectralTools.resample(wavelengths, nominalSpectrum, 40000) SpectralTools.write_2col_spectrum("observed.dat", newWave, newFlux)
edges = (plus !=0) & (minus != 0) IM[-2, edges] = (plus[edges]-minus[edges])/(0.2) nFilt = Synth.lineList.numLines-10 dims = IM.shape U,S,V = scipy.linalg.svd(IM) D = 1.0/(S[0:-nFilt]) S[-nFilt:] = 0.0 newS = numpy.zeros((dims[0], dims[1])) I = [i for i in range(dims[1])] for i in range(len(D)): newS[i][i] = D[i] S = newS.copy() CM = numpy.array(scipy.matrix(V.T.dot(S.T.dot(U.T)),dtype=numpy.float32)).T hdu = pyfits.PrimaryHDU(CM) hdu.writeto('CommandMatrix_new.fits', clobber=True) configFile = 'CMTeacher.cfg' Synth = MoogTools.Moog(configFile) Synth.lineList.writeLineLists() Synth.parameterFile.writeParFile() wavelengths, nominalSpectrum = Synth.run() newWave, newFlux = SpectralTools.resample(wavelengths, nominalSpectrum, 40000) SpectralTools.write_2col_spectrum("observed.dat", newWave, newFlux)
def calculateCM(SynthObj, nominalWave, nominalSpectrum): solarWl = SynthObj.solarSpectrum.wave solarFl = SynthObj.solarSpectrum.flux resolution = 45000.0 nLines = SynthObj.lineList.numLines nModes = 2*nLines+3 nFilt = nLines IM = numpy.zeros((nModes, len(solarWl))) stroke = numpy.ones(nModes) factor = numpy.ones(nModes) stroke[-1] = 0.005 # Continuum Shift stroke[-2] = 0.1 # WL Shift stroke[-3] = 0.01 # Smoothing for i in range(nLines): SynthObj.lineList.writeLineLists(i) wave, flux = SynthObj.run() stroke[i] = SynthObj.lineList.getGf(i)/5.0 stroke[i+nLines] = 0.1 """ while ((factor[i] < 0.9) | (factor[i] > 1.1)): stroke[i] *= factor[i] SynthObj.lineList.perturbGf(i, stroke[i]) SynthObj.lineList.writeLineLists(i,partial=True) wave, plus = SynthObj.run() wave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbGf(i, -2.0*stroke[i]) SynthObj.lineList.writeLineLists(i,partial=True) wave, minus = SynthObj.run() wave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbGf(i, stroke[i]) factor[i] = numpy.abs(target[i]/(numpy.min(plus)-numpy.min(minus))) print factor[i] raw_input() #""" for i in range(nLines): # log gf SynthObj.lineList.perturbGf(i, stroke[i]) SynthObj.lineList.writeLineLists(i) wave, plus = SynthObj.run() newWave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbGf(i, -2.0*stroke[i]) SynthObj.lineList.writeLineLists(i) wave, minus = SynthObj.run() newWave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbGf(i, stroke[i]) IM[i,:] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus - minus)/(2.0*stroke[i]), pad=0.0) factor[i] = numpy.max(numpy.abs(IM[i,:])) IM[i,:] /= factor[i] # damping SynthObj.lineList.perturbVdW(i, stroke[i+nLines]) SynthObj.lineList.writeLineLists(i) wave, plus = SynthObj.run() newWave, plus = SpectralTools.resample(wave, plus, resolution) SynthObj.lineList.perturbVdW(i, -2.0*stroke[i+nLines]) SynthObj.lineList.writeLineLists(i) wave, minus = SynthObj.run() newWave, minus = SpectralTools.resample(wave, minus, resolution) SynthObj.lineList.perturbVdW(i, stroke[i+nLines]) IM[i+nLines,:] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus-minus)/(2.0*stroke[i+nLines]), pad=0.0) factor[i+nLines] = numpy.max(numpy.abs(IM[i+nLines,:])) IM[i+nLines,:] /= factor[i+nLines] #Continuum Level plus = nominalSpectrum + stroke[-1] newWave, plus = SpectralTools.resample(nominalWave, plus, resolution) minus = nominalSpectrum - stroke[-1] newWave, minus = SpectralTools.resample(nominalWave, minus, resolution) IM[-1, :] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus-minus)/(2.0*stroke[-1]), pad=0.0) factor[-1] = numpy.max(numpy.abs(IM[-1,:])) IM[-1,:] /= factor[-1] #Wavelength Shift plus = SpectralTools.interpolate_spectrum(wave, wave-stroke[-2], nominalSpectrum, pad=True) newWave, plus = SpectralTools.resample(wave, plus, resolution) minus = SpectralTools.interpolate_spectrum(wave, wave+stroke[-2], nominalSpectrum, pad=True) newWave, minus = SpectralTools.resample(wave, minus, resolution) IM[-2, :] = SpectralTools.interpolate_spectrum(newWave, solarWl, (plus-minus)/(2.0*stroke[-2]), pad=0.0) factor[-2] = numpy.max(numpy.abs(IM[-2,:])) IM[-2,:] /= factor[-2] #""" #Instrumental Smoothing wavePlus, plus = SpectralTools.resample(wave, nominalSpectrum, resolution*(1.0+stroke[-3])) waveMinus, minus = SpectralTools.resample(wave, nominalSpectrum, resolution*(1.0-stroke[-3])) diffx, diffy = SpectralTools.diff_spectra(wavePlus, plus, waveMinus, minus, pad=True) IM[-3,:] = SpectralTools.interpolate_spectrum(diffx, solarWl, diffy/(2.0*stroke[-3]), pad=0.0) factor[-3] = numpy.max(numpy.abs(IM[-3,:])) IM[-3,:] /= factor[-3] #""" hdu = pyfits.PrimaryHDU(IM) hdu.writeto("InteractionMatrix.fits", clobber=True) #nFilt = Synth.lineList.numLines-2 dims = IM.shape U,S,V = scipy.linalg.svd(IM) D = 1.0/S #D = 1.0/(S[0:-nFilt]) #S[-nFilt:] = 0.0 newS = numpy.zeros((dims[0], dims[1])) I = [i for i in range(dims[1])] for i in range(len(D)): newS[i][i] = D[i] S = newS.copy() CM = numpy.array(scipy.matrix(V.T.dot(S.T.dot(U.T)),dtype=numpy.float32)).T fig = pyplot.figure(0) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax.plot(S) hdu = pyfits.PrimaryHDU(CM) hdu.writeto('CommandMatrix.fits', clobber=True) hdu = pyfits.PrimaryHDU(factor) hdu.writeto('scalingfactor.fits', clobber=True)
diffplot = False Synth = MoogTools.Moog(configFile) Synth.lineList.writeLineLists() Synth.parameterFile.writeParFile() solarWave = Synth.solarSpectrum.wave+0.1 solarFlux = Synth.solarSpectrum.flux+0.001+ numpy.random.randn(len(solarWave))*0.001 continuum = 0.0 wlOffset = 0.0 resolution = 45000 nominalWavelength, nominalSpectrum = Synth.run() wave, spectrum = SpectralTools.resample(nominalWavelength, nominalSpectrum, resolution) if True: calculateCM(Synth, nominalWavelength, nominalSpectrum) CM = pyfits.getdata("CommandMatrix.fits") factor = pyfits.getdata("scalingfactor.fits") f1 = pyplot.figure(0) f1.clear() ax1 = f1.add_axes([0.1, 0.1, 0.8, 0.8]) cb = ax1.matshow(CM, aspect='auto') blah = pyplot.colorbar(cb) f1.show()