Beispiel #1
0
def plotacfs(coords,
             times,
             configfile,
             maindir,
             cartcoordsys=True,
             indisp=True,
             acfdisp=True,
             fitdisp=True,
             filetemplate='acf',
             suptitle='ACF Comparison'):
    """ This will create a set of images that compare the input ISR acf to the
        output ISR acfs from the simulator.
        Inputs
        coords - An Nx3 numpy array that holds the coordinates of the desired points.
            times - A numpy list of times in seconds.
            configfile - The name of the configuration file used.
            cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if
            false then it is assumed that the coords are given in sphereical coordinates.
            specsfilename - (default None) The name of the file holding the input spectrum.
            acfname - (default None) The name of the file holding the estimated ACFs.
            filetemplate (default 'spec') This is the beginning string used to save the images.
    """
    #    indisp = specsfilename is not None
    #    acfdisp = acfname is not None

    sns.set_style("whitegrid")
    sns.set_context("notebook")
    acfname = os.path.join(maindir, 'ACF', '00lags.h5')
    ffit = os.path.join(maindir, 'Fitted', 'fitteddata.h5')

    specsfiledir = os.path.join(maindir, 'Spectrums')
    (sensdict, simparams) = readconfigfile(configfile)
    simdtype = simparams['dtype']
    npts = simparams['numpoints'] * 3.0
    amb_dict = simparams['amb_dict']
    if sp.ndim(coords) == 1:
        coords = coords[sp.newaxis, :]
    Nt = len(times)
    Nloc = coords.shape[0]
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    pulse = simparams['Pulse']
    ts = sensdict['t_s']
    tau1 = sp.arange(pulse.shape[-1]) * ts
    if indisp:
        dirlist = os.listdir(specsfiledir)
        timelist = sp.array([int(float(i.split()[0])) for i in dirlist])
        for itn, itime in enumerate(times):
            filear = sp.argwhere(timelist >= itime)
            if len(filear) == 0:
                filenum = len(timelist) - 1
            else:
                filenum = filear[0][0]
            specsfilename = os.path.join(specsfiledir, dirlist[filenum])
            Ionoin = IonoContainer.readh5(specsfilename)
            if itn == 0:
                specin = sp.zeros(
                    (Nloc, Nt, Ionoin.Param_List.shape[-1])).astype(
                        Ionoin.Param_List.dtype)
            omeg = Ionoin.Param_Names
            npts = Ionoin.Param_List.shape[-1]

            for icn, ic in enumerate(coords):
                if cartcoordsys:
                    tempin = Ionoin.getclosest(ic, times)[0]
                else:
                    tempin = Ionoin.getclosestsphere(ic, times)[0]


#                if sp.ndim(tempin)==1:
#                    tempin = tempin[sp.newaxis,:]
                specin[icn, itn] = tempin[0, :] / npts
    if acfdisp:
        Ionoacf = IonoContainer.readh5(acfname)
        ACFin = sp.zeros(
            (Nloc, Nt,
             Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype)

        omeg = sp.arange(-sp.ceil((npts + 1) / 2), sp.floor(
            (npts + 1) / 2)) / ts / npts
        for icn, ic in enumerate(coords):
            if cartcoordsys:
                tempin = Ionoacf.getclosest(ic, times)[0]
            else:
                tempin = Ionoacf.getclosestsphere(ic, times)[0]
            if sp.ndim(tempin) == 1:
                tempin = tempin[sp.newaxis, :]
            ACFin[icn] = tempin

    if fitdisp:
        Ionofit = IonoContainer.readh5(ffit)
        (omegfit, outspecsfit) = ISRspecmakeout(Ionofit.Param_List,
                                                sensdict['fc'], sensdict['fs'],
                                                simparams['species'], npts)
        Ionofit.Param_List = outspecsfit
        Ionofit.Param_Names = omegfit
        specfit = sp.zeros((Nloc, Nt, npts))
        for icn, ic in enumerate(coords):
            if cartcoordsys:
                tempin = Ionofit.getclosest(ic, times)[0]
            else:
                tempin = Ionofit.getclosestsphere(ic, times)[0]
            if sp.ndim(tempin) == 1:
                tempin = tempin[sp.newaxis, :]
            specfit[icn] = tempin / npts / npts

    nfig = int(sp.ceil(Nt * Nloc / 6.0))
    imcount = 0

    for i_fig in range(nfig):
        lines = [None] * 6
        labels = [None] * 6
        (figmplf, axmat) = plt.subplots(2, 3, figsize=(24, 18), facecolor='w')
        axvec = axmat.flatten()
        for iax, ax in enumerate(axvec):
            if imcount >= Nt * Nloc:
                break
            iloc = int(sp.floor(imcount / Nt))
            itime = int(imcount - (iloc * Nt))

            maxvec = []
            minvec = []

            if indisp:
                # apply ambiguity funciton to spectrum
                curin = specin[iloc, itime]
                (tau, acf) = spect2acf(omeg, curin)
                acf1 = scfft.ifftshift(acf)[:len(pulse)]
                rcs = acf1[0].real
                guess_acf = sp.dot(amb_dict['WttMatrix'], acf)
                guess_acf = guess_acf * rcs / guess_acf[0].real

                # fit to spectrums
                maxvec.append(guess_acf.real.max())
                maxvec.append(guess_acf.imag.max())
                minvec.append(acf1.real.min())
                minvec.append(acf1.imag.min())
                lines[0] = ax.plot(tau1 * 1e6,
                                   guess_acf.real,
                                   label='Input',
                                   linewidth=5)[0]
                labels[0] = 'Input ACF With Ambiguity Applied Real'
                lines[1] = ax.plot(tau1 * 1e6,
                                   guess_acf.imag,
                                   label='Input',
                                   linewidth=5)[0]
                labels[1] = 'Input ACF With Ambiguity Applied Imag'

            if fitdisp:
                curinfit = specfit[iloc, itime]
                (taufit, acffit) = spect2acf(omegfit, curinfit)
                rcsfit = curinfit.sum()
                guess_acffit = sp.dot(amb_dict['WttMatrix'], acffit)
                guess_acffit = guess_acffit * rcsfit / guess_acffit[0].real

                lines[2] = ax.plot(tau1 * 1e6,
                                   guess_acffit.real,
                                   label='Input',
                                   linewidth=5)[0]
                labels[2] = 'Fitted ACF real'
                lines[3] = ax.plot(tau1 * 1e6,
                                   guess_acffit.imag,
                                   label='Input',
                                   linewidth=5)[0]
                labels[3] = 'Fitted ACF Imag'
            if acfdisp:
                lines[4] = ax.plot(tau1 * 1e6,
                                   ACFin[iloc, itime].real,
                                   label='Output',
                                   linewidth=5)[0]
                labels[4] = 'Estimated ACF Real'
                lines[5] = ax.plot(tau1 * 1e6,
                                   ACFin[iloc, itime].imag,
                                   label='Output',
                                   linewidth=5)[0]
                labels[5] = 'Estimated ACF Imag'

                maxvec.append(ACFin[iloc, itime].real.max())
                maxvec.append(ACFin[iloc, itime].imag.max())
                minvec.append(ACFin[iloc, itime].real.min())
                minvec.append(ACFin[iloc, itime].imag.min())
            ax.set_xlabel('t in us')
            ax.set_ylabel('Amp')
            ax.set_title('Location {0}, Time {1}'.format(
                coords[iloc], times[itime]))
            ax.set_ylim(min(minvec), max(maxvec) * 1)
            ax.set_xlim([tau1.min() * 1e6, tau1.max() * 1e6])
            imcount = imcount + 1
        figmplf.suptitle(suptitle, fontsize=20)
        if None in labels:
            labels.remove(None)
            lines.remove(None)
        plt.figlegend(lines,
                      labels,
                      loc='lower center',
                      ncol=5,
                      labelspacing=0.)
        fname = filetemplate + '_{0:0>3}.png'.format(i_fig)
        plt.savefig(fname)
        plt.close(figmplf)
Beispiel #2
0
    def mult_iono(self, ionoin_list):
        """ 
            This will apply the forward model to the contents of an ionocontainer object. It is assuming that 
            this is an ionocontainer holding the spectra.
        """

        ntout = self.Time_Out.shape[0]
        nlout = self.Cart_Coords_Out.shape[0]
        blist_in, blist_out = self.blocklocs
        amb_dict = self.simparams['amb_dict']
        ambmat = amb_dict['WttMatrix']
        overlaps = self.overlaps

        t_s = self.sensdict['t_s']

        if isinstance(ionoin_list, list) or isinstance(ionoin_list, str):

            Iono_in = makeionocombined(ionoin_list)
        else:
            Iono_in = ionoin_list

        ionocart = Iono_in.Cart_Coords

        if self.simparams['numpoints'] == Iono_in.Param_List.shape[-1]:
            tau, acf = spect2acf(Iono_in.Param_Names, Iono_in.Param_List)
            np = ambmat.shape[0]
        else:
            acf = Iono_in.Param_List
            np = acf.shape[-1]
        np_in = acf.shape[-1]
        tau_out = t_s * sp.arange(np)
        outdata = sp.zeros((nlout, ntout, np), dtype=acf.dtype)
        assert sp.allclose(
            ionocart,
            self.Cart_Coords_In), "Spatial Coordinates need to be the same"

        for it_out in range(ntout):

            overlists = overlaps[it_out]
            irows = blist_out[it_out]
            curintimes = [i[0] for i in overlists]
            curintratio = [i[1] for i in overlists]
            if self.mattype.lower() == 'sim':
                curintimes = [curintimes[0]]
                curintratio = [1.]

            cur_outmat = self.RSTMat[irows[0]:irows[1], :]
            icols = blist_in[it_out]
            cur_mat = cur_outmat[:, icols[0]:icols[1]]

            for i_it, it_in in enumerate(curintimes):
                tempdata = sp.zeros((np_in, nlout), dtype=acf.dtype)
                for iparam in range(np_in):
                    tempdata[iparam] = cur_mat.dot(acf[:, it_in, iparam])
                if self.simparams['numpoints'] == Iono_in.Param_List.shape[-1]:
                    tempdata = sp.dot(ambmat, tempdata)

                outdata[:, it_out] = sp.transpose(
                    tempdata) * curintratio[i_it] + outdata[:, it_out]

        outiono = IonoContainer(self.Sphere_Coords_Out,
                                outdata,
                                times=self.Time_Out,
                                sensor_loc=Iono_in.Sensor_loc,
                                ver=1,
                                coordvecs=['r', 'theta', 'phi'],
                                paramnames=tau_out)
        return outiono
def plotacfs(coords,times,configfile,maindir,cartcoordsys = True, indisp=True,acfdisp= True,
             fitdisp=True, filetemplate='acf',suptitle = 'ACF Comparison'):

    """ This will create a set of images that compare the input ISR acf to the
    output ISR acfs from the simulator.
    Inputs
    coords - An Nx3 numpy array that holds the coordinates of the desired points.
    times - A numpy list of times in seconds.
    configfile - The name of the configuration file used.
    cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if
    false then it is assumed that the coords are given in sphereical coordinates.
    specsfilename - (default None) The name of the file holding the input spectrum.
    acfname - (default None) The name of the file holding the estimated ACFs.
    filetemplate (default 'spec') This is the beginning string used to save the images."""
#    indisp = specsfilename is not None
#    acfdisp = acfname is not None

    acfname = os.path.join(maindir,'ACF','00lags.h5')
    ffit = os.path.join(maindir,'Fitted','fitteddata.h5')

    specsfiledir = os.path.join(maindir,'Spectrums')
    (sensdict,simparams) = readconfigfile(configfile)
    simdtype = simparams['dtype']
    npts = simparams['numpoints']*3.0
    amb_dict = simparams['amb_dict']
    if sp.ndim(coords)==1:
        coords = coords[sp.newaxis,:]
    Nt = len(times)
    Nloc = coords.shape[0]
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    pulse = simparams['Pulse']
    ts = sensdict['t_s']
    tau1 = sp.arange(pulse.shape[-1])*ts
    if indisp:
        dirlist = os.listdir(specsfiledir)
        timelist = sp.array([int(i.split()[0]) for i in dirlist])
        for itn,itime in enumerate(times):
            filear = sp.argwhere(timelist>=itime)
            if len(filear)==0:
                filenum = len(timelist)-1
            else:
                filenum = filear[0][0]
            specsfilename = os.path.join(specsfiledir,dirlist[filenum])
            Ionoin = IonoContainer.readh5(specsfilename)
            if itn==0:
                specin = sp.zeros((Nloc,Nt,Ionoin.Param_List.shape[-1])).astype(Ionoin.Param_List.dtype)
            omeg = Ionoin.Param_Names
            npts = Ionoin.Param_List.shape[-1]

            for icn, ic in enumerate(coords):
                if cartcoordsys:
                    tempin = Ionoin.getclosest(ic,times)[0]
                else:
                    tempin = Ionoin.getclosestsphere(ic,times)[0]
#                if sp.ndim(tempin)==1:
#                    tempin = tempin[sp.newaxis,:]
                specin[icn,itn] = tempin[0,:]/npts
    if acfdisp:
        Ionoacf = IonoContainer.readh5(acfname)
        ACFin = sp.zeros((Nloc,Nt,Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype)

        omeg = sp.arange(-sp.ceil((npts+1)/2),sp.floor((npts+1)/2))/ts/npts
        for icn, ic in enumerate(coords):
            if cartcoordsys:
                tempin = Ionoacf.getclosest(ic,times)[0]
            else:
                tempin = Ionoacf.getclosestsphere(ic,times)[0]
            if sp.ndim(tempin)==1:
                tempin = tempin[sp.newaxis,:]
            ACFin[icn] = tempin


    if fitdisp:
        Ionofit = IonoContainer.readh5(ffit)
        (omegfit,outspecsfit) =ISRspecmakeout(Ionofit.Param_List,sensdict['fc'],sensdict['fs'],simparams['species'],npts)
        Ionofit.Param_List= outspecsfit
        Ionofit.Param_Names = omegfit
        specfit = sp.zeros((Nloc,Nt,npts))
        for icn, ic in enumerate(coords):
            if cartcoordsys:
                tempin = Ionofit.getclosest(ic,times)[0]
            else:
                tempin = Ionofit.getclosestsphere(ic,times)[0]
            if sp.ndim(tempin)==1:
                tempin = tempin[sp.newaxis,:]
            specfit[icn] = tempin/npts/npts

    nfig = int(sp.ceil(Nt*Nloc/6.0))
    imcount = 0

    for i_fig in range(nfig):
        lines = [None]*6
        labels = [None]*6
        (figmplf, axmat) = plt.subplots(2, 3,figsize=(24, 18), facecolor='w')
        axvec = axmat.flatten()
        for iax,ax in enumerate(axvec):
            if imcount>=Nt*Nloc:
                break
            iloc = int(sp.floor(imcount/Nt))
            itime = int(imcount-(iloc*Nt))

            maxvec = []
            minvec = []

            if indisp:
                # apply ambiguity funciton to spectrum
                curin = specin[iloc,itime]
                (tau,acf) = spect2acf(omeg,curin)
                acf1 = scfft.ifftshift(acf)[:len(pulse)]
                rcs=acf1[0].real
                guess_acf = sp.dot(amb_dict['WttMatrix'],acf)
                guess_acf = guess_acf*rcs/guess_acf[0].real

                # fit to spectrums
                maxvec.append(guess_acf.real.max())
                maxvec.append(guess_acf.imag.max())
                minvec.append(acf1.real.min())
                minvec.append(acf1.imag.min())
                lines[0]= ax.plot(tau1*1e6,guess_acf.real,label='Input',linewidth=5)[0]
                labels[0] = 'Input ACF With Ambiguity Applied Real'
                lines[1]= ax.plot(tau1*1e6,guess_acf.imag,label='Input',linewidth=5)[0]
                labels[1] = 'Input ACF With Ambiguity Applied Imag'

            if fitdisp:
                curinfit = specfit[iloc,itime]
                (taufit,acffit) = spect2acf(omegfit,curinfit)
                rcsfit=acffit[0].real
                guess_acffit = sp.dot(amb_dict['WttMatrix'],acffit)
                guess_acffit = guess_acffit*rcsfit/guess_acffit[0].real

                lines[2]= ax.plot(tau1*1e6,guess_acffit.real,label='Input',linewidth=5)[0]
                labels[2] = 'Fitted ACF real'
                lines[3]= ax.plot(tau1*1e6,guess_acffit.imag,label='Input',linewidth=5)[0]
                labels[3] = 'Fitted ACF Imag'
            if acfdisp:
                lines[4]=ax.plot(tau1*1e6,ACFin[iloc,itime].real,label='Output',linewidth=5)[0]
                labels[4] = 'Estimated ACF Real'
                lines[5]=ax.plot(tau1*1e6,ACFin[iloc,itime].imag,label='Output',linewidth=5)[0]
                labels[5] = 'Estimated ACF Imag'

                maxvec.append(ACFin[iloc,itime].real.max())
                maxvec.append(ACFin[iloc,itime].imag.max())
                minvec.append(ACFin[iloc,itime].real.min())
                minvec.append(ACFin[iloc,itime].imag.min())
            ax.set_xlabel('t in us')
            ax.set_ylabel('Amp')
            ax.set_title('Location {0}, Time {1}'.format(coords[iloc],times[itime]))
            ax.set_ylim(min(minvec),max(maxvec)*1)
            ax.set_xlim([tau1.min()*1e6,tau1.max()*1e6])
            imcount=imcount+1
        figmplf.suptitle(suptitle, fontsize=20)
        if None in labels:
            labels.remove(None)
            lines.remove(None)
        plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. )
        fname= filetemplate+'_{0:0>3}.png'.format(i_fig)
        plt.savefig(fname)
        plt.close(figmplf)
Beispiel #4
0
def main():
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    inifile= "/Users/Bodangles/Documents/Python/RadarDataSim/Testdata/PFISRExample.pickle"
    (sensdict,simparams) = readconfigfile(inifile)
    simdtype = simparams['dtype']
    sumrule = simparams['SUMRULE']
    npts = simparams['numpoints']
    amb_dict = simparams['amb_dict']
    # for spectrum
    ISS2 = ISRSpectrum(centerFrequency = 440.2*1e6, bMag = 0.4e-4, nspec=npts, sampfreq=sensdict['fs'],dFlag=True)
    ti = 2e3
    te = 2e3
    Ne = 1e11
    Ni = 1e11


    datablock90 = sp.array([[Ni,ti],[Ne,te]])
    species = simparams['species']

    (omega,specorig,rcs) = ISS2.getspecsep(datablock90, species,rcsflag = True)

    cur_filt = sp.sqrt(scfft.ifftshift(specorig*npts*npts*rcs/specorig.sum()))
    #for data
    Nrep = 10000
    pulse = sp.ones(14)
    lp_pnts = len(pulse)
    N_samps = 100
    minrg = -sumrule[0].min()
    maxrg = N_samps+lp_pnts-sumrule[1].max()
    Nrng2 = maxrg-minrg;
    out_data = sp.zeros((Nrep,N_samps+lp_pnts),dtype=simdtype)
    samp_num = sp.arange(lp_pnts)
    for isamp in range(N_samps):
        cur_pnts = samp_num+isamp
        cur_pulse_data = MakePulseDataRep(pulse,cur_filt,rep=Nrep)
        out_data[:,cur_pnts] = cur_pulse_data+out_data[:,cur_pnts]

    lagsData = CenteredLagProduct(out_data,numtype=simdtype,pulse =pulse)
    lagsData=lagsData/Nrep # divide out the number of pulses summed
    Nlags = lagsData.shape[-1]
    lagsDatasum = sp.zeros((Nrng2,Nlags),dtype=lagsData.dtype)
    for irngnew,irng in enumerate(sp.arange(minrg,maxrg)):
        for ilag in range(Nlags):
            lagsDatasum[irngnew,ilag] = lagsData[irng+sumrule[0,ilag]:irng+sumrule[1,ilag]+1,ilag].mean(axis=0)
    lagsDatasum=lagsDatasum/lp_pnts # divide out the pulse length
    (tau,acf) = spect2acf(omega,specorig)

    # apply ambiguity function
    tauint = amb_dict['Delay']
    acfinterp = sp.zeros(len(tauint),dtype=simdtype)

    acfinterp.real =spinterp.interp1d(tau,acf.real,bounds_error=0)(tauint)
    acfinterp.imag =spinterp.interp1d(tau,acf.imag,bounds_error=0)(tauint)
    # Apply the lag ambiguity function to the data
    guess_acf = sp.zeros(amb_dict['Wlag'].shape[0],dtype=sp.complex128)
    for i in range(amb_dict['Wlag'].shape[0]):
        guess_acf[i] = sp.sum(acfinterp*amb_dict['Wlag'][i])

#    pdb.set_trace()
    guess_acf = guess_acf*rcs/guess_acf[0].real

    # fit to spectrums
    spec_interm = scfft.fftshift(scfft.fft(guess_acf,n=npts))
    spec_final = spec_interm.real
    allspecs = scfft.fftshift(scfft.fft(lagsDatasum,n=len(spec_final),axis=-1),axes=-1)
#    allspecs = scfft.fftshift(scfft.fft(lagsDatasum,n=npts,axis=-1),axes=-1)
    fig = plt.figure()
    plt.plot(omega,spec_final.real,label='In',linewidth=5)
    plt.hold(True)
    plt.plot(omega,allspecs[40].real,label='Out',linewidth=5)
    plt.axis((omega.min(),omega.max(),0.0,2e11))
    plt.show(False)