コード例 #1
0
def makespectrums(basedir,configfile,remakealldata):
    """ This will make all of the spectra for a set of data and save it in a 
    folder in basedir called Spectrums. It is assumed that the data in the Origparams
    is time tagged in the title with a string seperated by a white space and the 
    rest of the file name. For example ~/DATA/Basedir/0 origdata.h5. 
    Inputs:
        basedir: A string for the directory that will hold all of the data for the simulation.
        configfile: The configuration file for the simulation.
         """

    dirio=('Origparams','Spectrums')
    inputdir = os.path.join(basedir,dirio[0])
    outputdir = os.path.join(basedir,dirio[1])
    # determine the list of h5 files in Origparams directory
    dirlist = glob.glob(os.path.join(inputdir,'*.h5'))
    # Make the lists of numbers and file names for the dictionary
    (listorder,timevector,filenumbering,timebeg,time_s) = IonoContainer.gettimes(dirlist)
    slist = [dirlist[ikey] for ikey in listorder]
    (sensdict,simparams) = readconfigfile(configfile)
    # Delete data 
    outfiles = glob.glob(os.path.join(outputdir,'*.h5'))
    for ifile in outfiles:
        os.remove(ifile)
                         
    for inum, curfile in zip(timebeg,slist):

        outfile = os.path.join(outputdir,str(inum)+' spectrum.h5')
        print('Processing file {0} starting at {1}\n'.format(os.path.split(curfile)[1]
            ,datetime.now()))
        curiono = IonoContainer.readh5(curfile)

        curiono.makespectruminstanceopen(specfuncs.ISRSspecmake,sensdict,
                                     simparams['numpoints']).saveh5(outfile)
        print('Finished file {0} starting at {1}\n'.format(os.path.split(curfile)[1],datetime.now()))
コード例 #2
0
 def loadfile(self):
     """Imports parameters from old files"""
     fn = tkFileDialog.askopenfilename(title="Load File",
                                       filetypes=[('INI', '.ini'),
                                                  ('PICKLE', '.pickle')])
     try:
         sensdict, simparams = readconfigfile(fn)
         rdrnames = {
             'PFISR': 'PFISR',
             'pfisr': 'PFISR',
             'risr': 'RISR-N',
             'RISR-N': 'RISR-N',
             'RISR': 'RISR-N'
         }
         currdr = rdrnames[sensdict['Name']]
         fitnfound = True
         for i in simparams:
             try:
                 if i == 'RangeLims':
                     self.paramdic[i][0].delete(0, END)
                     self.paramdic[i][1].delete(0, END)
                     self.paramdic[i][0].insert(0, str(simparams[i][0]))
                     self.paramdic[i][1].insert(0, str(simparams[i][1]))
                 elif i == 'species':
                     self.paramdic[i].delete(0, END)
                     string = ''
                     if isinstance(simparams[i], list):
                         for a in simparams[i]:
                             string += a
                             string += " "
                     else:
                         string = simparams[i]
                     self.paramdic[i].insert(0, string)
                 elif i == 'Pulselength' or i == 't_s':
                     self.paramdic[i].delete(0, END)
                     num = float(simparams[i]) * 10**6
                     self.paramdic[i].insert(0, str(num))
                 elif i == 'FitType':
                     self.fittype = simparams[i]
                     fitnfound = False
                 else:
                     self.paramdic[i].delete(0, END)
                     self.paramdic[i].insert(0, str(simparams[i]))
             except:
                 if simparams[i] == sp.complex128:
                     self.paramdic[i].set('complex128')
                 elif simparams[i] == sp.complex64:
                     self.paramdic[i].set('complex64')
                 elif i in self.paramdic:
                     self.paramdic[i].set(simparams[i])
         if fitnfound:
             self.fittype = 'Spectrum'
         self.pickbeams.var.set(currdr)
         self.pickbeams.Changefile()
         self.pickbeams.addbeamlist(simparams['angles'])
     except:
         print "Failed to import file."
コード例 #3
0
ファイル: setupGUI.py プロジェクト: hhuangmeso/RadarDataSim
 def loadfile(self):
     """Imports parameters from old files"""
     fn = tkFileDialog.askopenfilename(title="Load File",filetypes=[('INI','.ini'),('PICKLE','.pickle')])
     try:
         sensdict,simparams = readconfigfile(fn)
         rdrnames = {'PFISR':'PFISR','pfisr':'PFISR','risr':'RISR-N','RISR-N':'RISR-N','RISR':'RISR-N'}
         currdr = rdrnames[sensdict['Name']]
         fitnfound = True
         for i in simparams:
             try:
                 if i=='RangeLims':
                     self.paramdic[i][0].delete(0,END)
                     self.paramdic[i][1].delete(0,END)
                     self.paramdic[i][0].insert(0,str(simparams[i][0]))
                     self.paramdic[i][1].insert(0,str(simparams[i][1]))
                 elif i=='species':
                     self.paramdic[i].delete(0,END)
                     string=''
                     if isinstance(simparams[i],list):
                         for a in simparams[i]:
                             string+=a
                             string+=" "
                     else:
                         string = simparams[i]
                     self.paramdic[i].insert(0,string)
                 elif i=='Pulselength' or i=='t_s':
                     self.paramdic[i].delete(0,END)
                     num = float(simparams[i])*10**6
                     self.paramdic[i].insert(0,str(num))
                 elif i== 'FitType':
                     self.fittype = simparams[i]
                     fitnfound=False
                 else:
                     self.paramdic[i].delete(0,END)
                     self.paramdic[i].insert(0,str(simparams[i]))
             except:
                 if simparams[i]==sp.complex128:
                     self.paramdic[i].set('complex128')
                 elif simparams[i]==sp.complex64:
                     self.paramdic[i].set('complex64')
                 elif i in self.paramdic:
                     self.paramdic[i].set(simparams[i])
         if fitnfound:
             self.fittype = 'Spectrum'
         self.pickbeams.var.set(currdr)
         self.pickbeams.Changefile()
         self.pickbeams.addbeamlist(simparams['angles'])
     except:
         print "Failed to import file."
コード例 #4
0
def main(plist=None,
         functlist=['spectrums', 'radardata', 'fitting', 'analysis', 'stats']):
    """ This function will call other functions to create the input data, config
        file and run the radar data sim. The path for the simulation will be 
        created in the Testdata directory in the RadarDataSim module. The new
        folder will be called BasicTest. The simulation is a long pulse simulation
        will the desired number of pulses from the user.
        Inputs
            npulse - Number of pulses for the integration period, default==100.
            functlist - The list of functions for the RadarDataSim to do.
    """
    if plist is None:
        plist = sp.array([50, 100, 200, 500, 1000, 2000, 5000])
    if isinstance(plist, list):
        plist = sp.array(plist)
    curloc = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))
    testpath = os.path.join(os.path.split(curloc)[0], 'Testdata', 'StatsTest')

    if not os.path.isdir(testpath):
        os.mkdir(testpath)
    functlist_default = ['spectrums', 'radardata', 'fitting']
    check_list = sp.array([i in functlist for i in functlist_default])
    check_run = sp.any(check_list)
    functlist_red = sp.array(functlist_default)[check_list].tolist()
    allfolds = []
    #    rsystools = []
    for ip in plist:
        foldname = 'Pulses_{:04d}'.format(ip)
        curfold = os.path.join(testpath, foldname)
        allfolds.append(curfold)
        if not os.path.isdir(curfold):
            os.mkdir(curfold)

            configfilesetup(curfold, ip)
        makedata(curfold)
        config = os.path.join(curfold, 'stats.ini')
        (sensdict, simparams) = readconfigfile(config)
        #        rtemp = RadarSys(sensdict,simparams['Rangegatesfinal'],ip)
        #        rsystools.append(rtemp.rms(sp.array([1e12]),sp.array([2.5e3]),sp.array([2.5e3])))
        if check_run:
            runsim(functlist_red, curfold, os.path.join(curfold, 'stats.ini'),
                   True)
        if 'analysis' in functlist:
            analysisdump(curfold, config)
        if 'stats' in functlist:
            makehist(curfold, ip)
コード例 #5
0
def configfilesetup(testpath, npulses):
    """ This will create the configureation file given the number of pulses for 
        the test. This will make it so that there will be 12 integration periods 
        for a given number of pulses.
        Input
            testpath - The location of the data.
            npulses - The number of pulses. 
    """

    curloc = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))
    defcon = os.path.join(curloc, 'statsbase.ini')

    (sensdict, simparams) = readconfigfile(defcon)
    tint = simparams['IPP'] * npulses
    ratio1 = tint / simparams['Tint']
    simparams['Tint'] = ratio1 * simparams['Tint']
    simparams['Fitinter'] = ratio1 * simparams['Fitinter']
    simparams['TimeLim'] = ratio1 * simparams['TimeLim']

    simparams['startfile'] = 'startfile.h5'
    makeconfigfile(os.path.join(testpath, 'stats.ini'), simparams['Beamlist'],
                   sensdict['Name'], simparams)
コード例 #6
0
ファイル: runsim.py プロジェクト: hhuangmeso/RadarDataSim
def makespectrums(basedir, configfile, remakealldata):

    dirio = ("Origparams", "Spectrums")
    inputdir = os.path.join(basedir, dirio[0])
    outputdir = os.path.join(basedir, dirio[1])
    dirlist = glob.glob(os.path.join(inputdir, "*.h5"))
    numlist = [os.path.splitext(os.path.split(x)[-1])[0] for x in dirlist]
    numdict = {numlist[i]: dirlist[i] for i in range(len(dirlist))}
    slist = sorted(numlist, key=ke)

    (sensdict, simparams) = readconfigfile(configfile)

    for inum in slist:

        outfile = os.path.join(outputdir, inum + " spectrum.h5")
        curfile = numdict[inum]
        print("Processing file {} starting at {}\n".format(os.path.split(curfile)[1], datetime.now()))
        curiono = IonoContainer.readh5(curfile)
        #        if curiono.Time_Vector[0,0]==1e-6:
        #            curiono.Time_Vector[0,0] = 0.0
        #        curiono.coordreduce(coordlims)
        #        curiono.saveh5(os.path.join(inputdir,inum+' red.h5'))
        curiono.makespectruminstanceopen(specfuncs.ISRSspecmake, sensdict, simparams["numpoints"]).saveh5(outfile)
        print("Finished file {} starting at {}\n".format(os.path.split(curfile)[1], datetime.now()))
コード例 #7
0
def analysisdump(maindir,configfile,suptitle=None):
    """ """
    plotdir = os.path.join(maindir,'AnalysisPlots')
    if not os.path.isdir(plotdir):
        os.mkdir(plotdir)

    #plot spectrums
    filetemplate1 = os.path.join(maindir,'AnalysisPlots','Spec')
    filetemplate3 = os.path.join(maindir,'AnalysisPlots','ACF')

    (sensdict,simparams) = readconfigfile(configfile)
    angles = simparams['angles']
    ang_data = sp.array([[iout[0],iout[1]] for iout in angles])
    zenang = ang_data[sp.argmax(ang_data[:,1])]
    rnggates = simparams['Rangegatesfinal']
    rngchoices = sp.linspace(sp.amin(rnggates),sp.amax(rnggates),4)
    angtile = sp.tile(zenang,(len(rngchoices),1))
    coords = sp.column_stack((sp.transpose(rngchoices),angtile))
    times = simparams['Timevec']


    filetemplate2= os.path.join(maindir,'AnalysisPlots','Params')
    if simparams['Pulsetype'].lower()=='barker':
        if suptitle is None:
            plotbeamparameters(times,configfile,maindir,params=['Ne'],filetemplate=filetemplate2,werrors=True)
        else:
            plotbeamparameters(times,configfile,maindir,params=['Ne'],filetemplate=filetemplate2,suptitle=suptitle,werrors=True)
    else:
        if suptitle is None:
            plotspecs(coords,times,configfile,maindir,cartcoordsys = False, filetemplate=filetemplate1)
            plotacfs(coords,times,configfile,maindir,cartcoordsys = False, filetemplate=filetemplate3)
            plotbeamparameters(times,configfile,maindir,params=['Ne','Nepow','Te','Ti'],filetemplate=filetemplate2,werrors=True)
        else:
            plotspecs(coords,times,configfile,maindir,cartcoordsys = False, filetemplate=filetemplate1,suptitle=suptitle)
            plotacfs(coords,times,configfile,maindir,cartcoordsys = False, filetemplate=filetemplate3,suptitle=suptitle)
            plotbeamparameters(times,configfile,maindir,params=['Ne','Nepow','Te','Ti'],filetemplate=filetemplate2,suptitle=suptitle,werrors=True)
コード例 #8
0
ファイル: analysisplots.py プロジェクト: yangjian615/SimISR
def fitsurfaceplot(paramdict,
                   plotvals,
                   configfile,
                   y_acf,
                   yerr=None,
                   filetemplate='fitsurfs',
                   suptitle='Fit Surfaces'):
    """ This will create a fit surface plot. 
        Inputs
        paramdict - A dictionary with the followign key value pairs.
            Ne - Array of possible electron density values.
            Te - Array of possible electron tempreture values.
            Ti - Array of possible ion tempreture values.
            frac - Array of possible fraction shares of the ion make up.
        plotvals - A dictionary with key value pars. 
            setparam - A string that describes he parameter thats set.
            xparam - The parameter that's varied along the x axis of the image.
            yparam - The parameter that's varied along the y axis of the image.  
            indx - The index from the paramdict for the set variable. 
        configfile - The file thats used for the simulation.
        y_acf - the complex ACF used to create the errors.
        yerr - The standard deviation of the acf measurement.
        filetemplate - The template on how the file will be named.
        suptitle - The super title for the plots. 
    """
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    (sensdict, simparams) = readconfigfile(configfile)
    specs = simparams['species']
    nspecs = len(specs)

    # make param lists
    paramlist = [[]] * (2 * nspecs + 1)
    paramlist[2 * (nspecs - 1)] = paramdict['Ne']
    paramlist[2 * (nspecs - 1) + 1] = paramdict['Te']

    if 'frac' in paramdict.keys():
        frac = paramdict['frac']
    else:
        frac = [[1. / (nspecs - 1)]] * (nspecs - 1)

    for ispec in range(nspecs - 1):
        paramlist[2 * ispec] = frac[ispec]
        paramlist[2 * ispec + 1] = paramdict['Ti'][ispec]

    if 'Vi' in paramdict.keys():
        paramlist[-1] = paramdict['Vi']
    else:
        paramlist[-1] = [0.]

    pvals = {
        'Ne': 2 * (nspecs - 1),
        'Te': 2 * (nspecs - 1) + 1,
        'Ti': 1,
        'frac': 0
    }

    fitsurfs = makefitsurf(paramlist, y_acf, sensdict, simparams, yerr)
    quad = (3, 3)
    i_fig = 0
    for iplt, idict in enumerate(plotvals):
        iaxn = sp.mod(iplt, sp.prod(quad))

        if iaxn == 0:
            (figmplf, axmat) = plt.subplots(quad[0],
                                            quad[1],
                                            figsize=(20, 15),
                                            facecolor='w')
            axvec = axmat.flatten()

        setstr = idict['setparam']
        xstr = idict['xparam']
        ystr = idict['yparam']
        mloc = pvals[setstr]
        xdim = pvals[xstr]
        ydim = pvals[ystr]
        setval = paramlist[setstr][idict['indx']]
        transarr = sp.arange(2 * nspecs + 1).tolist()
        transarr.remove(mloc)
        transarr.remove(xdim)
        transarr.remove(ydim)
        transarr = [mloc, ydim, xdim] + transarr
        fitupdate = sp.transpose(fitsurfs, transarr)
        while fitupdate.ndim > 3:
            fitupdate = sp.nanmean(fitupdate, dim=-1)
        Z1 = fitupdate[idict['indx']]
        iax = axvec[iaxn]
        xvec = paramdict[xstr]
        yvec = paramdict[ystr]
        [Xmat, Ymat] = sp.meshgrid(xvec, yvec)

        iax.pcolor(Xmat,
                   Ymat,
                   Z1,
                   norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()))
        iax.xlabel = xstr
        iax.ylabel = ystr
        iax.title('{0} at {0}'.format(setstr, setval))
        if iaxn == sp.prod(quad) - 1:
            figmplf.suptitle(suptitle, fontsize=20)
            fname = filetemplate + '_{0:0>4}.png'.format(i_fig)
            plt.savefig(fname)
            plt.close(figmplf)
            i_fig += 1
コード例 #9
0
ファイル: testcomp.py プロジェクト: yangjian615/SimISR
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)
コード例 #10
0
ファイル: singleacf.py プロジェクト: yangjian615/SimISR
def fitcheck(repall=[100]):
    x_0 = sp.array([[[1.00000000e+11, 2.00000000e+03],
                     [1.00000000e+11, 2.00000000e+03]],
                    [[5.00000000e+11, 2.00000000e+03],
                     [5.00000000e+11, 2.00000000e+03]],
                    [[1.00000000e+11, 3.00000000e+03],
                     [1.00000000e+11, 2.00000000e+03]],
                    [[1.00000000e+11, 2.00000000e+03],
                     [1.00000000e+11, 3.00000000e+03]]])
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    x_0_red = x_0[0].flatten()
    x_0_red[-2] = x_0_red[-1]
    x_0_red[-1] = 0.
    configfile = 'statsbase.ini'
    (sensdict, simparams) = readconfigfile(configfile)
    ambdict = simparams['amb_dict']
    pulse = simparams['Pulse']
    l_p = len(pulse)
    Nlags = l_p
    lagv = sp.arange(l_p)
    ntypes = x_0.shape[0]
    nspec = 128
    nrg = 64
    des_pnt = 16
    ISpec = ISRSpectrum(nspec=nspec, sampfreq=50e3)
    species = ['O+', 'e-']
    spvtime = sp.zeros((ntypes, nspec))
    lablist = ['Normal', 'E-Ne', 'E-Ti', 'E-Te']
    v_i = 0
    fitfunc = ISRSfitfunction

    sumrule = simparams['SUMRULE']
    Nrg1 = nrg + 1 - l_p
    minrg = -sumrule[0].min()
    maxrg = Nrg1 - sumrule[1].max()
    Nrg2 = maxrg - minrg

    for i in range(ntypes):
        f, curspec, rcs = ISpec.getspecsep(x_0[i], species, v_i, rcsflag=True)
        specsum = sp.absolute(curspec).sum()
        spvtime[i] = rcs * curspec * nspec**2 / specsum

    acforig = scfft.ifft(scfft.ifftshift(spvtime, axes=1), axis=1) / nspec
    acfamb = sp.dot(ambdict['WttMatrix'],
                    scfft.fftshift(acforig, axes=1).transpose()).transpose()
    specamb = scfft.fftshift(scfft.fft(acfamb, n=nspec, axis=1), axes=1)

    fig, axmat = plt.subplots(2, 2)
    axvec = axmat.flatten()

    figs, axmats = plt.subplots(2, 2)
    axvecs = axmats.flatten()

    for i in range(ntypes):
        ax = axvec[i]
        ax.plot(lagv, acfamb[i].real, label='Input')
        ax.set_title(lablist[i])
        axs = axvecs[i]
        axs.plot(f * 1e-3, specamb[i].real, label='Input', linewidth=4)
        axs.set_title(lablist[i])

    for irep, rep1 in enumerate(repall):
        rawdata = sp.zeros((ntypes, rep1, nrg), dtype=sp.complex128)
        acfest = sp.zeros((ntypes, Nrg1, l_p), dtype=rawdata.dtype)
        acfestsr = sp.zeros((ntypes, Nrg2, l_p), dtype=rawdata.dtype)
        specest = sp.zeros((ntypes, nspec), dtype=rawdata.dtype)
        for i in range(ntypes):
            for j in range(nrg - (l_p - 1)):
                rawdata[i, :, j:j + l_p] = MakePulseDataRepLPC(
                    pulse, spvtime[i], 20, rep1) + rawdata[i, :, j:j + l_p]
            acfest[i] = CenteredLagProduct(rawdata[i], pulse=pulse) / (rep1)
            for irngnew, irng in enumerate(sp.arange(minrg, maxrg)):
                for ilag in range(Nlags):
                    acfestsr[i][irngnew,
                                ilag] = acfest[i][irng +
                                                  sumrule[0, ilag]:irng +
                                                  sumrule[1, ilag] + 1,
                                                  ilag].mean(axis=0)
            ax = axvec[i]
            ax.plot(lagv,
                    acfestsr[i, des_pnt].real / l_p,
                    label='Np = {0}'.format(rep1))
            if irep == len(repall) - 1:
                ax.legend()
            specest[i] = scfft.fftshift(
                scfft.fft(acfestsr[i, des_pnt], n=nspec))
            axs = axvecs[i]
            axs.plot(f * 1e-3,
                     specest[i].real / l_p,
                     label='Np = {0}'.format(rep1),
                     linewidth=4)
            if irep == len(repall) - 1:
                axs.legend()
        print('Parameters fitted after {0} pulses'.format(rep1))
        print('Ni                Ti             Te                 Vi')
        for i in range(ntypes):
            d_func = (acfestsr[i, des_pnt] / l_p, sensdict, simparams)
            (x, cov_x, infodict, mesg,
             ier) = scipy.optimize.leastsq(func=fitfunc,
                                           x0=x_0_red,
                                           args=d_func,
                                           full_output=True)
            print(x)
        print(' ')
    fig.suptitle('ACF with Sum Rule')
    fig.savefig('pulsetestacf.png', dpi=400)
    plt.close(fig)
    figs.suptitle('Spectrum Full Array')
    figs.savefig('pulsetestspec.png', dpi=400)
    plt.close(figs)
コード例 #11
0
def plotbeamparameters(times,configfile,maindir,params=['Ne'],indisp=True,fitdisp = True,filetemplate='params',suptitle = 'Parameter Comparison',werrors=False):
    """ """
    sns.set_style("whitegrid")
    sns.set_context("notebook")
#    rc('text', usetex=True)
    ffit = os.path.join(maindir,'Fitted','fitteddata.h5')
    inputfiledir = os.path.join(maindir,'Origparams')
    (sensdict,simparams) = readconfigfile(configfile)

    paramslower = [ip.lower() for ip in params]
    Nt = len(times)
    Np = len(params)

    if fitdisp:
        Ionofit = IonoContainer.readh5(ffit)
        dataloc = Ionofit.Sphere_Coords
        pnames = Ionofit.Param_Names
        pnameslower = sp.array([ip.lower() for ip in pnames.flatten()])
        p2fit = [sp.argwhere(ip==pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower]
        time2fit = [None]*Nt
        for itn,itime in enumerate(times):
            filear = sp.argwhere(Ionofit.Time_Vector>=itime)
            if len(filear)==0:
                filenum = len(Ionofit.Time_Vector)-1
            else:
                filenum = filear[0][0]
            time2fit[itn] = filenum

    angles = dataloc[:,1:]
    rng = sp.unique(dataloc[:,0])
    b = np.ascontiguousarray(angles).view(np.dtype((np.void, angles.dtype.itemsize * angles.shape[1])))
    _, idx, invidx = np.unique(b, return_index=True,return_inverse=True)

    beamlist = angles[idx]

    Nb = beamlist.shape[0]

    if indisp:
        dirlist = glob.glob(os.path.join(inputfiledir,'*.h5'))
        filesonly= [os.path.splitext(os.path.split(ifile)[-1])[0] for ifile in dirlist]

        timelist = sp.array([int(i.split()[0]) for i in filesonly])
        time2file = [None]*Nt
        for itn,itime in enumerate(times):
            filear = sp.argwhere(timelist>=itime)
            if len(filear)==0:
                filenum = len(timelist)-1
            else:
                filenum = filear[0][0]
            time2file[itn] = filenum


    nfig = int(sp.ceil(Nt*Nb*Np/9.0))
    imcount = 0
    curfilenum = -1
    for i_fig in range(nfig):
        lines = [None]*2
        labels = [None]*2
        (figmplf, axmat) = plt.subplots(3, 3,figsize=(20, 15), facecolor='w')
        axvec = axmat.flatten()
        for iax,ax in enumerate(axvec):
            if imcount>=Nt*Nb*Np:
                break
            itime = int(sp.floor(imcount/Nb/Np))
            iparam = int(imcount/Nb-Np*itime)
            ibeam = int(imcount-(itime*Np*Nb+iparam*Nb))

            curbeam = beamlist[ibeam]

            altlist = sp.sin(curbeam[1]*sp.pi/180.)*rng

            if fitdisp:

                indxkep = np.argwhere(invidx==ibeam)[:,0]

                curfit = Ionofit.Param_List[indxkep,time2fit[itime],p2fit[iparam]]
                rng_fit= dataloc[indxkep,0]
                alt_fit = rng_fit*sp.sin(curbeam[1]*sp.pi/180.)
                errorexist = 'n'+paramslower[iparam] in pnameslower
                if errorexist and werrors:
                    eparam = sp.argwhere( 'n'+paramslower[iparam]==pnameslower)[0][0]
                    curerror = Ionofit.Param_List[indxkep,time2fit[itime],eparam]
                    lines[1]=ax.errorbar(curfit, alt_fit, xerr=curerror,fmt='-.',c='g')[0]
                else:
                    lines[1]= ax.plot(curfit,alt_fit,marker='.',c='g')[0]
                labels[1] = 'Fitted Parameters'

            if indisp:
                filenum = time2file[itime]
                if curfilenum!=filenum:
                    curfilenum=filenum
                    datafilename = dirlist[filenum]
                    Ionoin = IonoContainer.readh5(datafilename)
                    if 'ti' in paramslower:
                        Ionoin = maketi(Ionoin)
                    pnames = Ionoin.Param_Names
                    pnameslowerin = sp.array([ip.lower() for ip in pnames.flatten()])

                curparm = paramslower[iparam]
                if curparm == 'nepow':
                    curparm = 'ne'
                prmloc = sp.argwhere(curparm==pnameslowerin)

                if prmloc.size !=0:
                    curprm = prmloc[0][0]

                curcoord = sp.zeros(3)
                curcoord[1:] = curbeam
                curdata = sp.zeros(len(rng))
                for irngn, irng in enumerate(rng):
                    curcoord[0] = irng
                    tempin = Ionoin.getclosestsphere(curcoord,times)[0]
                    Ntloc = tempin.shape[0]
                    tempin = sp.reshape(tempin,(Ntloc,len(pnameslowerin)))
                    curdata[irngn] = tempin[0,curprm]
                lines[0]= ax.plot(curdata,altlist,marker='o',c='b')[0]
                labels[0] = 'Input Parameters'
                if curparm!='ne':
                    ax.set(xlim=[0.25*sp.amin(curdata),2.5*sp.amax(curdata)])
            if curparm=='ne':
                ax.set_xscale('log')

            ax.set_xlabel(params[iparam])
            ax.set_ylabel('Alt km')
            ax.set_title('{0} vs Altitude, Time: {1}s Az: {2}$^o$ El: {3}$^o$'.format(params[iparam],times[itime],*curbeam))
            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)
コード例 #12
0
ファイル: analysisplots.py プロジェクト: yangjian615/SimISR
def analysisdump(maindir, configfile, suptitle=None):
    """ This function will perform all of the plotting functions in this module
        given the main directory that all of the files live. 
        Inputs
            maindir - The directory for the simulation. 
            configfile - The name of the configuration file used.
            suptitle - The supertitle used on the files. 
    """
    plotdir = os.path.join(maindir, 'AnalysisPlots')
    if not os.path.isdir(plotdir):
        os.mkdir(plotdir)

    #plot spectrums
    filetemplate1 = os.path.join(maindir, 'AnalysisPlots', 'Spec')
    filetemplate3 = os.path.join(maindir, 'AnalysisPlots', 'ACF')
    filetemplate4 = os.path.join(maindir, 'AnalysisPlots', 'AltvTime')
    (sensdict, simparams) = readconfigfile(configfile)
    angles = simparams['angles']
    ang_data = sp.array([[iout[0], iout[1]] for iout in angles])
    if not sensdict['Name'].lower() in ['risr', 'pfisr']:
        ang_data_temp = ang_data.copy()
        beamlistlist = sp.array(simparams['outangles']).astype(int)
        ang_data = sp.array(
            [ang_data_temp[i].mean(axis=0) for i in beamlistlist])

    zenang = ang_data[sp.argmax(ang_data[:, 1])]
    rnggates = simparams['Rangegatesfinal']
    rngchoices = sp.linspace(sp.amin(rnggates), sp.amax(rnggates), 4)
    angtile = sp.tile(zenang, (len(rngchoices), 1))
    coords = sp.column_stack((sp.transpose(rngchoices), angtile))
    times = simparams['Timevec']

    filetemplate2 = os.path.join(maindir, 'AnalysisPlots', 'Params')
    if simparams['Pulsetype'].lower() == 'barker':
        params = ['Ne']
        if suptitle is None:
            plotbeamparametersv2(times,
                                 configfile,
                                 maindir,
                                 params=params,
                                 filetemplate=filetemplate2,
                                 werrors=True)
        else:
            plotbeamparametersv2(times,
                                 configfile,
                                 maindir,
                                 params=params,
                                 filetemplate=filetemplate2,
                                 suptitle=suptitle,
                                 werrors=True)
    else:
        params = ['Ne', 'Nepow', 'Te', 'Ti', 'Vi']
        if suptitle is None:
            plotspecs(coords,
                      times,
                      configfile,
                      maindir,
                      cartcoordsys=False,
                      filetemplate=filetemplate1)
            plotacfs(coords,
                     times,
                     configfile,
                     maindir,
                     cartcoordsys=False,
                     filetemplate=filetemplate3)
            plotbeamparametersv2(times,
                                 configfile,
                                 maindir,
                                 params=params,
                                 filetemplate=filetemplate2,
                                 werrors=True)
            beamvstime(configfile,
                       maindir,
                       params=params,
                       filetemplate=filetemplate4)
        else:
            plotspecs(coords,
                      times,
                      configfile,
                      maindir,
                      cartcoordsys=False,
                      filetemplate=filetemplate1,
                      suptitle=suptitle)
            plotacfs(coords,
                     times,
                     configfile,
                     maindir,
                     cartcoordsys=False,
                     filetemplate=filetemplate3,
                     suptitle=suptitle)
            plotbeamparametersv2(times,
                                 configfile,
                                 maindir,
                                 params=params,
                                 filetemplate=filetemplate2,
                                 suptitle=suptitle,
                                 werrors=True)
            beamvstime(configfile,
                       maindir,
                       params=params,
                       filetemplate=filetemplate4,
                       suptitle=suptitle)
コード例 #13
0
ファイル: analysisplots.py プロジェクト: yangjian615/SimISR
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)
コード例 #14
0
ファイル: analysisplots.py プロジェクト: yangjian615/SimISR
def plotbeamparametersv2(times,
                         configfile,
                         maindir,
                         fitdir='Fitted',
                         params=['Ne'],
                         filetemplate='params',
                         suptitle='Parameter Comparison',
                         werrors=False,
                         nelog=True):
    """ This function will plot the desired parameters for each beam along range.
        The values of the input and measured parameters will be plotted
        Inputs 
            Times - A list of times that will be plotted.
            configfile - The INI file with the simulation parameters that will be useds.
            maindir - The directory the images will be saved in.
            params - List of Parameter names that will be ploted. These need to match
                in the ionocontainer names.
            filetemplate - The first part of a the file names.
            suptitle - The supertitle for the plots.
            werrors - A bools that determines if the errors will be plotted.
    """
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    #    rc('text', usetex=True)
    ffit = os.path.join(maindir, fitdir, 'fitteddata.h5')
    inputfiledir = os.path.join(maindir, 'Origparams')
    (sensdict, simparams) = readconfigfile(configfile)

    paramslower = [ip.lower() for ip in params]
    Nt = len(times)
    Np = len(params)

    #Read in fitted data

    Ionofit = IonoContainer.readh5(ffit)
    dataloc = Ionofit.Sphere_Coords
    pnames = Ionofit.Param_Names
    pnameslower = sp.array([ip.lower() for ip in pnames.flatten()])
    p2fit = [
        sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None
        for ip in paramslower
    ]
    time2fit = [None] * Nt

    for itn, itime in enumerate(times):
        filear = sp.argwhere(Ionofit.Time_Vector >= itime)
        if len(filear) == 0:
            filenum = len(Ionofit.Time_Vector) - 1
        else:
            filenum = filear[0][0]
        time2fit[itn] = filenum
    times_int = [Ionofit.Time_Vector[i] for i in time2fit]

    # determine the beams
    angles = dataloc[:, 1:]
    rng = sp.unique(dataloc[:, 0])
    b = np.ascontiguousarray(angles).view(
        np.dtype((np.void, angles.dtype.itemsize * angles.shape[1])))
    _, idx, invidx = np.unique(b, return_index=True, return_inverse=True)

    beamlist = angles[idx]

    Nb = beamlist.shape[0]

    # Determine which imput files are to be used.

    dirlist = glob.glob(os.path.join(inputfiledir, '*.h5'))
    filesonly = [
        os.path.splitext(os.path.split(ifile)[-1])[0] for ifile in dirlist
    ]
    sortlist, outime, outfilelist, timebeg, timelist_s = IonoContainer.gettimes(
        dirlist)
    timelist = sp.array([int(i.split()[0]) for i in filesonly])
    time2file = [None] * Nt

    time2intime = [None] * Nt
    # go through times find files and then times in files
    for itn, itime in enumerate(times):

        filear = sp.argwhere(timelist >= itime)
        if len(filear) == 0:
            filenum = [len(timelist) - 1]
        else:
            filenum = filear[0]

        flist1 = []
        timeinflist = []
        for ifile in filenum:
            filetimes = timelist_s[ifile]
            log1 = (filetimes[:, 0] >= times_int[itn][0]) & (filetimes[:, 0] <
                                                             times_int[itn][1])
            log2 = (filetimes[:, 1] > times_int[itn][0]) & (filetimes[:, 1] <=
                                                            times_int[itn][1])
            log3 = (filetimes[:, 0] <= times_int[itn][0]) & (filetimes[:, 1] >
                                                             times_int[itn][1])
            log4 = (filetimes[:, 0] > times_int[itn][0]) & (filetimes[:, 1] <
                                                            times_int[itn][1])
            curtimes1 = sp.where(log1 | log2 | log3 | log4)[0].tolist()
            flist1 = flist1 + [ifile] * len(curtimes1)
            timeinflist = timeinflist + curtimes1
        time2intime[itn] = timeinflist
        time2file[itn] = flist1
    nfig = int(sp.ceil(Nt * Nb * Np / 9.0))
    imcount = 0
    curfilenum = -1
    # Loop for the figures
    for i_fig in range(nfig):
        lines = [None] * 2
        labels = [None] * 2
        (figmplf, axmat) = plt.subplots(3, 3, figsize=(20, 15), facecolor='w')
        axvec = axmat.flatten()
        # loop that goes through each axis loops through each parameter, beam
        # then time.
        for iax, ax in enumerate(axvec):
            if imcount >= Nt * Nb * Np:
                break
            itime = int(sp.floor(imcount / Nb / Np))
            iparam = int(imcount / Nb - Np * itime)
            ibeam = int(imcount - (itime * Np * Nb + iparam * Nb))
            curbeam = beamlist[ibeam]

            altlist = sp.sin(curbeam[1] * sp.pi / 180.) * rng

            curparm = paramslower[iparam]
            # Use Ne from input to compare the ne derived from the power.
            if curparm == 'nepow':
                curparm_in = 'ne'
            else:
                curparm_in = curparm

            curcoord = sp.zeros(3)
            curcoord[1:] = curbeam

            for iplot, filenum in enumerate(time2file[itime]):

                if curfilenum != filenum:
                    curfilenum = filenum
                    datafilename = dirlist[filenum]
                    Ionoin = IonoContainer.readh5(datafilename)
                    if ('ti' in paramslower) or ('vi' in paramslower):
                        Ionoin = maketi(Ionoin)
                    pnames = Ionoin.Param_Names
                    pnameslowerin = sp.array(
                        [ip.lower() for ip in pnames.flatten()])
                prmloc = sp.argwhere(curparm_in == pnameslowerin)
                if prmloc.size != 0:
                    curprm = prmloc[0][0]
                # build up parameter vector bs the range values by finding the closest point in space in the input
                curdata = sp.zeros(len(rng))
                for irngn, irng in enumerate(rng):
                    curcoord[0] = irng
                    tempin = Ionoin.getclosestsphere(curcoord)[0][
                        time2intime[itime]]
                    Ntloc = tempin.shape[0]
                    tempin = sp.reshape(tempin, (Ntloc, len(pnameslowerin)))
                    curdata[irngn] = tempin[0, curprm]
                #actual plotting of the input data
                lines[0] = ax.plot(curdata,
                                   altlist,
                                   marker='o',
                                   c='b',
                                   linewidth=2)[0]
                labels[0] = 'Input Parameters'
            # Plot fitted data for the axis

            indxkep = np.argwhere(invidx == ibeam)[:, 0]
            curfit = Ionofit.Param_List[indxkep, time2fit[itime],
                                        p2fit[iparam]]
            rng_fit = dataloc[indxkep, 0]
            alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.)
            errorexist = 'n' + paramslower[iparam] in pnameslower
            if errorexist and werrors:
                eparam = sp.argwhere('n' +
                                     paramslower[iparam] == pnameslower)[0][0]
                curerror = Ionofit.Param_List[indxkep, time2fit[itime], eparam]
                lines[1] = ax.errorbar(curfit,
                                       alt_fit,
                                       xerr=curerror,
                                       fmt='-.',
                                       c='g',
                                       linewidth=2)[0]
            else:
                lines[1] = ax.plot(curfit,
                                   alt_fit,
                                   marker='o',
                                   c='g',
                                   linewidth=2)[0]
            labels[1] = 'Fitted Parameters'
            # get and plot the input data

            numplots = len(time2file[itime])

            # set the limit for the parameter
            if curparm_in != 'ne':
                ax.set(xlim=[0.75 * sp.amin(curfit), 1.25 * sp.amax(curfit)])
            if curparm == 'vi':
                ax.set(xlim=[
                    -1.25 * sp.amax(sp.absolute(curfit)), 1.25 *
                    sp.amax(sp.absolute(curfit))
                ])
            if (curparm_in == 'ne') and nelog:
                ax.set_xscale('log')

            ax.set_xlabel(params[iparam])
            ax.set_ylabel('Alt km')
            ax.set_title(
                '{0} vs Altitude, Time: {1}s Az: {2}$^o$ El: {3}$^o$'.format(
                    params[iparam], times[itime], *curbeam))
            imcount = imcount + 1
        # save figure
        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)
コード例 #15
0
ファイル: analysisplots.py プロジェクト: yangjian615/SimISR
def beamvstime(configfile,
               maindir,
               params=['Ne'],
               filetemplate='AltvTime',
               suptitle='Alt vs Time'):
    """ This will create a altitude time image for the data for ionocontainer files
        that are in sphereical coordinates.
        Inputs 
            Times - A list of times that will be plotted.
            configfile - The INI file with the simulation parameters that will be useds.
            maindir - The directory the images will be saved in.
            params - List of Parameter names that will be ploted. These need to match
                in the ionocontainer names.
            filetemplate - The first part of a the file names.
            suptitle - The supertitle for the plots.
    """
    sns.set_style("whitegrid")
    sns.set_context("notebook")
    #    rc('text', usetex=True)
    (sensdict, simparams) = readconfigfile(configfile)

    paramslower = [ip.lower() for ip in params]
    Np = len(params)
    inputfile = os.path.join(maindir, 'Fitted', 'fitteddata.h5')

    Ionofit = IonoContainer.readh5(inputfile)
    times = Ionofit.Time_Vector
    Nt = len(times)
    dataloc = Ionofit.Sphere_Coords
    pnames = Ionofit.Param_Names
    pnameslower = sp.array([ip.lower() for ip in pnames.flatten()])
    p2fit = [
        sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None
        for ip in paramslower
    ]

    angles = dataloc[:, 1:]
    b = np.ascontiguousarray(angles).view(
        np.dtype((np.void, angles.dtype.itemsize * angles.shape[1])))
    _, idx, invidx = np.unique(b, return_index=True, return_inverse=True)

    beamlist = angles[idx]

    Nb = beamlist.shape[0]

    newfig = True
    imcount = 0
    ifig = -1
    for iparam in range(Np):
        for ibeam in range(Nb):

            if newfig:
                (figmplf, axmat) = plt.subplots(3,
                                                3,
                                                figsize=(20, 15),
                                                facecolor='w',
                                                sharex=True,
                                                sharey=True)
                axvec = axmat.flatten()
                newfig = False
                ix = 0
                ifig += 1

            ax = axvec[ix]

            curbeam = beamlist[ibeam]
            curparm = paramslower[iparam]
            if curparm == 'nepow':
                curparm = 'ne'

            indxkep = np.argwhere(invidx == ibeam)[:, 0]
            rng_fit = dataloc[indxkep, 0]
            rngargs = np.argsort(rng_fit)
            rng_fit = rng_fit[rngargs]
            alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.)
            curfit = Ionofit.Param_List[indxkep, :, p2fit[iparam]]
            curfit = curfit[rngargs]
            Tmat, Amat = np.meshgrid(times[:, 0], alt_fit)
            image = ax.pcolor(Tmat, Amat, curfit, cmap='jet')
            if curparm == 'ne':
                image.set_norm(colors.LogNorm(vmin=1e9, vmax=5e12))
                cbarstr = params[iparam] + ' m-3'
            else:
                image.set_norm(colors.PowerNorm(gamma=1., vmin=500, vmax=3e3))
                cbarstr = params[iparam] + ' K'

            if ix > 5:
                ax.set_xlabel("Time in s")
            if sp.mod(ix, 3) == 0:
                ax.set_ylabel('Alt km')
            ax.set_title('{0} vs Altitude, Az: {1}$^o$ El: {2}$^o$'.format(
                params[iparam], *curbeam))
            imcount = imcount + 1

            ix += 1
            if ix == 9 or ibeam + 1 == Nb:
                cbar_ax = figmplf.add_axes([.91, .3, .06, .4])
                cbar = plt.colorbar(image, cax=cbar_ax)
                cbar.set_label(cbarstr)
                figmplf.suptitle(suptitle, fontsize=20)
                figmplf.tight_layout(rect=[0, .05, .9, .95])
                fname = filetemplate + '_{0:0>3}.png'.format(ifig)
                plt.savefig(fname)
                plt.close(figmplf)
                newfig = True
コード例 #16
0
def fitsurfaceplot(paramdict,plotvals,configfile,y_acf,yerr=None,filetemplate='fitsurfs',suptitle = 'Fit Surfaces'):

    (sensdict,simparams) = readconfigfile(configfile)
    specs = simparams['species']
    nspecs = len(specs)

    # make param lists
    paramlist = [[]]*(2*nspecs+1)
    paramlist[2*(nspecs-1)] =paramdict['Ne']
    paramlist[2*(nspecs-1)+1] =paramdict['Te']

    if 'frac' in paramdict.keys():
        frac = paramdict['frac']
    else:
        frac = [[1./(nspecs-1)]]*(nspecs-1)

    for ispec in range(nspecs-1):
        paramlist[2*ispec] =frac[ispec]
        paramlist[2*ispec+1] =  paramdict['Ti'][ispec]

    if 'Vi' in paramdict.keys():
        paramlist[-1] = paramdict['Vi']
    else:
        paramlist[-1] =[0.]

    pvals = {'Ne':2*(nspecs-1),'Te':2*(nspecs-1)+1,'Ti':1,'frac':0}

    fitsurfs= makefitsurf(paramlist,y_acf,sensdict,simparams,yerr)
    quad = (3,3)
    i_fig=0
    for iplt, idict in enumerate(plotvals):
        iaxn = sp.mod(iplt,sp.prod(quad))

        if iaxn==0:
            (figmplf, axmat) = plt.subplots(quad[0],quad[1],figsize=(20, 15), facecolor='w')
            axvec = axmat.flatten()

        setstr = idict['setparam']
        xstr = idict['xparam']
        ystr = idict['yparam']
        mloc = pvals[setstr]
        xdim = pvals[xstr]
        ydim = pvals[ystr]
        setval = paramlist[setstr][idict['indx']]
        transarr = sp.arange(2*nspecs+1).tolist()
        transarr.remove(mloc)
        transarr.remove(xdim)
        transarr.remove(ydim)
        transarr = [mloc,ydim,xdim] +transarr
        fitupdate = sp.transpose(fitsurfs,transarr)
        while fitupdate.ndim>3:
            fitupdate = sp.nanmean(fitupdate,dim=-1)
        Z1 = fitupdate[idict['indx']]
        iax = axvec[iaxn]
        xvec = paramdict[xstr]
        yvec = paramdict[ystr]
        [Xmat,Ymat]= sp.meshgrid(xvec,yvec)

        iax.pcolor(Xmat,Ymat,Z1,norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()))
        iax.xlabel=xstr
        iax.ylabel=ystr
        iax.title('{0} at {0}'.format(setstr,setval))
        if iaxn ==sp.prod(quad)-1:
            figmplf.suptitle(suptitle, fontsize=20)
            fname= filetemplate+'_{0:0>4}.png'.format(i_fig)
            plt.savefig(fname)
            plt.close(figmplf)
            i_fig+=1
コード例 #17
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

    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)