Esempio n. 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()))
def makeinputh5(Iono,basedir):
    """This will make a h5 file for the IonoContainer that can be used as starting
    points for the fitter. The ionocontainer taken will be average over the x and y dimensions
    of space to make an average value of the parameters for each altitude.
    Inputs
    Iono - An instance of the Ionocontainer class that will be averaged over so it can
    be used for fitter starting points.
    basdir - A string that holds the directory that the file will be saved to.
    """
    # Get the parameters from the original data
    Param_List = Iono.Param_List
    dataloc = Iono.Cart_Coords
    times = Iono.Time_Vector
    velocity = Iono.Velocity
    zlist,idx = sp.unique(dataloc[:,2],return_inverse=True)
    siz = list(Param_List.shape[1:])
    vsiz = list(velocity.shape[1:])

    datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist))
    outdata = sp.zeros([len(zlist)]+siz)
    outvel = sp.zeros([len(zlist)]+vsiz)
    #  Do the averaging across space
    for izn,iz in enumerate(zlist):
        arr = sp.argwhere(idx==izn)
        outdata[izn]=sp.mean(Param_List[arr],axis=0)
        outvel[izn]=sp.mean(velocity[arr],axis=0)

    Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0,
                            paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel)
    Ionoout.saveh5(os.path.join(basedir,'startdata.h5'))
Esempio n. 3
0
def makeinputh5(Iono,basedir):
    """This will make a h5 file for the IonoContainer that can be used as starting
    points for the fitter. The ionocontainer taken will be average over the x and y dimensions
    of space to make an average value of the parameters for each altitude.
    Inputs
    Iono - An instance of the Ionocontainer class that will be averaged over so it can
    be used for fitter starting points.
    basdir - A string that holds the directory that the file will be saved to.
    """
    # Get the parameters from the original data
    Param_List = Iono.Param_List
    dataloc = Iono.Cart_Coords
    times = Iono.Time_Vector
    velocity = Iono.Velocity
    zlist,idx = sp.unique(dataloc[:,2],return_inverse=True)
    siz = list(Param_List.shape[1:])
    vsiz = list(velocity.shape[1:])

    datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist))
    outdata = sp.zeros([len(zlist)]+siz)
    outvel = sp.zeros([len(zlist)]+vsiz)
    #  Do the averaging across space
    for izn,iz in enumerate(zlist):
        arr = sp.argwhere(idx==izn)
        outdata[izn]=sp.mean(Param_List[arr],axis=0)
        outvel[izn]=sp.mean(velocity[arr],axis=0)

    Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0,
                            paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel)
    Ionoout.saveh5(os.path.join(basedir,'startdata.h5'))
Esempio n. 4
0
def makeinputh5(Iono, basedir):
    Param_List = Iono.Param_List
    dataloc = Iono.Cart_Coords
    times = Iono.Time_Vector
    velocity = Iono.Velocity
    zlist, idx = sp.unique(dataloc[:, 2], return_inverse=True)
    siz = list(Param_List.shape[1:])
    vsiz = list(velocity.shape[1:])

    datalocsave = sp.column_stack(
        (sp.zeros_like(zlist), sp.zeros_like(zlist), zlist))
    outdata = sp.zeros([len(zlist)] + siz)
    outvel = sp.zeros([len(zlist)] + vsiz)

    for izn, iz in enumerate(zlist):
        arr = sp.argwhere(idx == izn)
        outdata[izn] = sp.mean(Param_List[arr], axis=0)
        outvel[izn] = sp.mean(velocity[arr], axis=0)

    Ionoout = IonoContainer(datalocsave,
                            outdata,
                            times,
                            Iono.Sensor_loc,
                            ver=0,
                            paramnames=Iono.Param_Names,
                            species=Iono.Species,
                            velocity=outvel)
    Ionoout.saveh5(os.path.join(basedir, 'startdata.h5'))
Esempio n. 5
0
def fitdata(basedir, configfile, optintputs):
    dirio = ("ACF", "Fitted")
    inputdir = os.path.join(basedir, dirio[0])
    outputdir = os.path.join(basedir, dirio[1])

    dirlist = glob.glob(os.path.join(inputdir, "*lags.h5"))
    dirlistsig = glob.glob(os.path.join(inputdir, "*sigs.h5"))

    Ionoin = IonoContainer.readh5(dirlist[0])
    Ionoinsig = IonoContainer.readh5(dirlistsig[0])
    fitterone = Fitterionoconainer(Ionoin, Ionoinsig, configfile)
    (fitteddata, fittederror) = fitterone.fitdata(
        ISRSfitfunction, startvalfunc, exinputs=[fitterone.simparams["startfile"]]
    )

    if fitterone.simparams["Pulsetype"].lower() == "barker":
        paramlist = fitteddata
        species = fitterone.simparams["species"]
        paranamsf = ["Ne"]
    else:
        (Nloc, Ntimes, nparams) = fitteddata.shape
        fittederronly = fittederror[:, :, range(nparams), range(nparams)]

        paramnames = []
        species = fitterone.simparams["species"]
        Nions = len(species) - 1
        Nis = fitteddata[:, :, 0 : Nions * 2 : 2]
        Tis = fitteddata[:, :, 1 : Nions * 2 : 2]
        Nisum = sp.nansum(Nis, axis=2)[:, :, sp.newaxis]
        Tisum = sp.nansum(Nis * Tis, axis=2)[:, :, sp.newaxis]
        Ti = Tisum / Nisum

        nNis = fittederronly[:, :, 0 : Nions * 2 : 2]
        nTis = fittederronly[:, :, 1 : Nions * 2 : 2]
        nNisum = sp.sqrt(sp.nansum(Nis * nNis ** 2, axis=2))[:, :, sp.newaxis] / Nisum
        nTisum = sp.sqrt(sp.nansum(Nis * nTis ** 2, axis=2))[:, :, sp.newaxis]
        nTi = nTisum / Nisum

        paramlist = sp.concatenate((fitteddata, Nisum, Ti, fittederronly, nNisum, nTi), axis=2)
        for isp in species[:-1]:
            paramnames.append("Ni_" + isp)
            paramnames.append("Ti_" + isp)
        paramnames = paramnames + ["Ne", "Te", "Vi", "Nepow", "Ni", "Ti"]
        paramnamese = ["n" + ip for ip in paramnames]
        paranamsf = sp.array(paramnames + paramnamese)

    Ionoout = IonoContainer(
        Ionoin.Sphere_Coords,
        paramlist,
        Ionoin.Time_Vector,
        ver=1,
        coordvecs=Ionoin.Coord_Vecs,
        paramnames=paranamsf,
        species=species,
    )

    outfile = os.path.join(outputdir, "fitteddata.h5")
    Ionoout.saveh5(outfile)
Esempio n. 6
0
def makedata(testpath):
    """ This will make the input data for the test case. The data will have the 
        default set of parameters Ne=Ne=1e11 and Te=Ti=2000.
        Inputs
            testpath - Directory that will hold the data.
            
    """
    finalpath = os.path.join(testpath, 'Origparams')
    if not os.path.isdir(finalpath):
        os.mkdir(finalpath)
    data = SIMVALUES
    z = sp.linspace(50., 1e3, 50)
    nz = len(z)
    params = sp.tile(data[sp.newaxis, sp.newaxis, :, :], (nz, 1, 1, 1))
    coords = sp.column_stack((sp.ones(nz), sp.ones(nz), z))
    species = ['O+', 'e-']
    times = sp.array([[0, 1e3]])
    vel = sp.zeros((nz, 1, 3))
    Icont1 = IonoContainer(coordlist=coords,
                           paramlist=params,
                           times=times,
                           sensor_loc=sp.zeros(3),
                           ver=0,
                           coordvecs=['x', 'y', 'z'],
                           paramnames=None,
                           species=species,
                           velocity=vel)

    finalfile = os.path.join(finalpath, '0 stats.h5')
    Icont1.saveh5(finalfile)
    # set start temp to 1000 K.
    Icont1.Param_List[:, :, :, 1] = 1e3
    Icont1.saveh5(os.path.join(testpath, 'startfile.h5'))
Esempio n. 7
0
def makeinputh5(Iono,basedir):
    Param_List = Iono.Param_List
    dataloc = Iono.Cart_Coords
    times = Iono.Time_Vector
    velocity = Iono.Velocity
    zlist,idx = sp.unique(dataloc[:,2],return_inverse=True)
    siz = list(Param_List.shape[1:])
    vsiz = list(velocity.shape[1:])

    datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist))
    outdata = sp.zeros([len(zlist)]+siz)
    outvel = sp.zeros([len(zlist)]+vsiz)

    for izn,iz in enumerate(zlist):
        arr = sp.argwhere(idx==izn)
        outdata[izn]=sp.mean(Param_List[arr],axis=0)
        outvel[izn]=sp.mean(velocity[arr],axis=0)

    Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0,
                            paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel)
    Ionoout.saveh5(os.path.join(basedir,'startdata.h5'))
Esempio n. 8
0
def main():

    curpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    testpath = os.path.join(os.path.split(curpath)[0],'Testdata','MatrixTest')
    origparamsdir = os.path.join(testpath,'Origparams')
    if not os.path.exists(testpath):
        os.mkdir(testpath)
        print "Making a path for matrix test at "+testpath
    if not os.path.exists(origparamsdir):
        os.mkdir(origparamsdir)
        print "Making a path for testdata at "+origparamsdir

    # clear everything out
    folderlist = ['Origparams','Spectrums','Radardata','ACF','Fitted']
    for ifl in folderlist:
        flist = glob.glob(os.path.join(testpath,ifl,'*.h5'))
        for ifile in flist:
            os.remove(ifile)
    # Make Config file
    configname = os.path.join(testpath,'config.ini')
    if ~os.path.isfile(configname):
        makedefaultfile(configname)

    # make the coordinates
    xvec = sp.zeros((1))
    yvec = sp.zeros((1))
    zvec = sp.arange(50.0,900.0,2.0)
    # Mesh grid is set up in this way to allow for use in MATLAB with a simple reshape command
    xx,zz,yy = sp.meshgrid(xvec,zvec,yvec)
    coords = sp.zeros((xx.size,3))
    coords[:,0] = xx.flatten()
    coords[:,1] = yy.flatten()
    coords[:,2] = zz.flatten()

    Icont1 = MakeTestIonoclass(testv=True,testtemp=True,coords=coords)
    Icont1.saveh5(os.path.join(origparamsdir,'0 testiono.h5'))
    Icont1.saveh5(os.path.join(testpath,'startdata.h5'))
    funcnamelist=['spectrums']
    runsim.main(funcnamelist,testpath,configname,True)
    Ispec = IonoContainer.readh5(os.path.join(testpath,'Spectrums','0 testiono spectrum.h5'))
    Spec = Ispec.Param_List.real
    acflen = 14

    # get matrix
    outmat = makematPA(Ispec.Sphere_Coords,Ispec.Time_Vector,configname)
    (outn,inn) = outmat.shape
    acfs = sp.ifft(scfft.ifftshift(Spec,axes=-1),axis=-1)[:,:,:acflen]
    acfout = sp.zeros((outn,acflen)).astype(acfs.dtype)

    acfs = sp.reshape(acfs,(inn,acflen),order='F')

    for i in range(acflen):
        acfout[:,i] = sp.dot(outmat,acfs[:,i])
Esempio n. 9
0
def applymat(basedir,configfile,optinputs):
    """ This function apply the matrix version of the space time ambiugty function
        to the ACFs and save the outcome in h5 files within the directory ACFMat.
        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 = ('Spectrums','Mat','ACFMat')
    inputdir = os.path.join(basedir,dirio[0])
    outputdir2 = os.path.join(basedir,dirio[2])
    
    dirlist = glob.glob(os.path.join(inputdir,'*.h5'))
    (listorder,timevector,filenumbering,timebeg,time_s) = IonoContainer.gettimes(dirlist)
    Ionolist = [dirlist[ikey] for ikey in listorder]
    RSTO = RadarSpaceTimeOperator(Ionolist,configfile,timevector,mattype='matrix')
    Ionoout = RSTO.mult_iono(Ionolist)
    outfile=os.path.join(outputdir2,'00lags.h5')
    Ionoout.saveh5(outfile)
Esempio n. 10
0
def startvalfunc(Ne_init, loc, time, exinputs):
    """ """

    Ionoin = IonoContainer.readh5(exinputs[0])

    numel = sp.prod(Ionoin.Param_List.shape[-2:]) + 1

    xarray = sp.zeros((loc.shape[0], len(time), numel))
    for ilocn, iloc in enumerate(loc):
        (datast, vel) = Ionoin.getclosest(iloc, time)[:2]
        datast[:, -1, 0] = Ne_init[ilocn, :]
        ionoden = datast[:, :-1, 0]
        ionodensum = sp.repeat(sp.sum(ionoden, axis=-1)[:, sp.newaxis], ionoden.shape[-1], axis=-1)
        ionoden = sp.repeat(Ne_init[ilocn, :, sp.newaxis], ionoden.shape[-1], axis=-1) * ionoden / ionodensum
        datast[:, :-1, 0] = ionoden
        xarray[ilocn, :, :-1] = sp.reshape(datast, (len(time), numel - 1))
        locmag = sp.sqrt(sp.sum(iloc * iloc))
        ilocmat = sp.repeat(iloc[sp.newaxis, :], len(time), axis=0)
        xarray[ilocn, :, -1] = sp.sum(vel * ilocmat) / locmag

    return xarray
Esempio n. 11
0
def makeradardata(basedir,configfile,remakealldata):
    """ This function will make the radar data and create the acf estimates.
    Inputs:
        basedir: A string for the directory that will hold all of the data for the simulation.
        configfile: The configuration file for the simulation.
        remakealldata: A bool that determines if the radar data is remade. If false
            only the acfs will be estimated using the radar that is already made."""
            
    dirio = ('Spectrums','Radardata','ACF')
    inputdir = os.path.join(basedir,dirio[0])
    outputdir = os.path.join(basedir,dirio[1])
    outputdir2 = os.path.join(basedir,dirio[2])

    # 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
    if len(dirlist)>0:
        (listorder,timevector,filenumbering,timebeg,time_s) = IonoContainer.gettimes(dirlist)

        Ionodict = {timebeg[itn]:dirlist[it] for itn, it in enumerate(listorder)}
    else:
        Ionodict = {0:os.path.join(inputdir,'00.h5')}
    # Find all of the raw data files
    radardatalist = glob.glob(os.path.join(outputdir,'*RawData.h5'))
    if radardatalist and (not remakealldata):
        # XXX need to work on time stuff
        outlist2 = radardatalist
    else:
        outlist2 = None
        
    # create the radar data file class
    rdata = RadarDataFile(Ionodict,configfile,outputdir,outfilelist=outlist2)
    # From the ACFs and uncertainties
    (ionoout,ionosig) = rdata.processdataiono()
    # save the acfs and uncertianties in ionocontainer h5 files.
    ionoout.saveh5(os.path.join(outputdir2,'00lags.h5'))
    ionosig.saveh5(os.path.join(outputdir2,'00sigs.h5'))
    return ()
Esempio n. 12
0
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()))
Esempio n. 13
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)
Esempio n. 14
0
        Titemp[auglist] = Titemp[auglist]*ratiodiff
        Tetemp[auglist] = Tetemp[auglist]*ratiodiff
        curprofile[auglist] = val
        #densitys
        params[:,it,0,0] = curprofile
        params[:,it,1,0] = curprofile
        #tempreturs
        params[:,it,0,1] = Tetemp
        params[:,it,1,1] = Titemp
        #veclocity
        params[:,it,0,2] = Vi_all
        params[:,it,1,2] = Vi_all
        #charge
        params[:,it,0,3] = 1.0
        params[:,it,1,3] = 1.0
        #mass
        params[:,it,0,4] = 1.0
        params[:,it,1,4] = 16.0
        #collision frequency
        params[:,it,0,5] = 0.0
        params[:,it,1,5] = 0.0


    Icont1 = IonoContainer(coordlist=coords,paramlist=params,times = timevec,coordvecs=coordVecs)
#    Icont1.savemat(os.path.join(testpath,'pathwtempdoppler.mat'))

    sensdict = sensconst.getConst('risr')

    Icont2 = Icont1.makespectruminstance(sensdict,128)
#    Icont2.savemat(os.path.join(testpath,'pa'))
Esempio n. 15
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)
Esempio n. 16
0
def fitdata(basedir,configfile,optinputs):
    """ This function will run the fitter on the estimated ACFs saved in h5 files.
        Inputs:
        basedir: A string for the directory that will hold all of the data for the simulation.
        configfile: The configuration file for the simulation.
        optinputs:A string that helps determine the what type of acfs will be fitted.
         """
    # determine the input folders which can be ACFs from the full simulation
    dirdict = {'fitting':('ACF','Fitted'),'fittingmat':('ACFMat','FittedMat'),
               'fittinginv':('ACFInv','FittedInv'),'fittingmatinv':('ACFMatInv','FittedMatInv')}
    dirio = dirdict[optinputs[0]]
    inputdir = os.path.join(basedir,dirio[0])
    outputdir = os.path.join(basedir,dirio[1])
    fitlist=optinputs[1]
    if len(optinputs)>2:
        exstr=optinputs[2]
    else:
        exstr=''
    dirlist = glob.glob(os.path.join(inputdir,'*lags{0}.h5'.format(exstr)))
    dirlistsig = glob.glob(os.path.join(inputdir,'*sigs{0}.h5'.format(exstr)))

    Ionoin=IonoContainer.readh5(dirlist[0])
    if len(dirlistsig)==0:
        Ionoinsig=None
    else:
        Ionoinsig=IonoContainer.readh5(dirlistsig[0])
    fitterone = Fitterionoconainer(Ionoin,Ionoinsig,configfile)
    
    (fitteddata,fittederror,funcevals) = fitterone.fitdata(ISRSfitfunction,fitterone.simparams['startfile'],fittimes=fitlist)


    if fitterone.simparams['Pulsetype'].lower() == 'barker':
        paramlist=fitteddata
        species = fitterone.simparams['species']
        paranamsf=['Ne']
    else:
        (Nloc,Ntimes,nparams)=fitteddata.shape
        fittederronly = sp.sqrt(fittederror)
        paramnames = []
        species = fitterone.simparams['species']
        # Seperate Ti and put it in as an element of the ionocontainer.
        Ti = fitteddata[:,:,1]

        nTi =  fittederronly[:,:,1]  
        
        paramlist = sp.concatenate((fitteddata,Ti[:,:,sp.newaxis],fittederronly,nTi[:,:,sp.newaxis],funcevals[:,:,sp.newaxis]),axis=2)
        for isp in species[:-1]:
            paramnames.append('Ni_'+isp)
            paramnames.append('Ti_'+isp)
        paramnames = paramnames+['Ne','Te','Vi','Nepow','Ti']
        paramnamese = ['n'+ip for ip in paramnames]
        paranamsf = sp.array(paramnames+paramnamese+['FuncEvals'])

    if fitlist is None:
        timevec = Ionoin.Time_Vector
    else:
        if len(fitlist)==0:
            timevec = Ionoin.Time_Vector
        else:
            timevec = Ionoin.Time_Vector[fitlist]
    # This requires
    if set(Ionoin.Coord_Vecs)=={'x','y','z'}:
        newver=0
        Ionoout=IonoContainer(Ionoin.Cart_Coords,paramlist,timevec,ver =newver,coordvecs = Ionoin.Coord_Vecs, paramnames=paranamsf,species=species)
    elif set(Ionoin.Coord_Vecs)=={'r','theta','phi'}:
        newver=1
        Ionoout=IonoContainer(Ionoin.Sphere_Coords,paramlist,timevec,ver =newver,coordvecs = Ionoin.Coord_Vecs, paramnames=paranamsf,species=species)
    

    outfile = os.path.join(outputdir,'fitteddata{0}.h5'.format(exstr))
    Ionoout.saveh5(outfile)
Esempio n. 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)
Esempio n. 18
0
def makehistdata(params, maindir):
    """ This will make the histogram data for the statistics.
        Inputs
            params -  A list of parameters that will have statistics created
            maindir - The directory that the simulation data is held.
        Outputs
            datadict - A dictionary with the data values in numpy arrays. The keys are param names.
            errordict - A dictionary with the data values in numpy arrays. The keys are param names.
            errdictrel -  A dictionary with the error values in numpy arrays, normalized by the correct value. The keys are param names.
    """
    ffit = os.path.join(maindir, 'Fitted', 'fitteddata.h5')
    inputfiledir = os.path.join(maindir, 'Origparams')

    paramslower = [ip.lower() for ip in params]
    eparamslower = ['n' + ip.lower() for ip in params]

    # set up data dictionary

    errordict = {ip: [] for ip in params}
    errordictrel = {ip: [] for ip in params}
    #Read in fitted data

    Ionofit = IonoContainer.readh5(ffit)
    times = Ionofit.Time_Vector

    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
    ]

    datadict = {
        ip: Ionofit.Param_List[:, :, p2fit[ipn]].flatten()
        for ipn, ip in enumerate(params)
    }

    ep2fit = [
        sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None
        for ip in eparamslower
    ]

    edatadict = {
        ip: Ionofit.Param_List[:, :, ep2fit[ipn]].flatten()
        for ipn, ip in enumerate(params)
    }
    # Determine which input files are to be used.

    dirlist = glob.glob(os.path.join(inputfiledir, '*.h5'))
    sortlist, outime, filelisting, timebeg, timelist_s = IonoContainer.gettimes(
        dirlist)
    time2files = []
    for itn, itime in enumerate(times):
        log1 = (outime[:, 0] >= itime[0]) & (outime[:, 0] < itime[1])
        log2 = (outime[:, 1] > itime[0]) & (outime[:, 1] <= itime[1])
        log3 = (outime[:, 0] <= itime[0]) & (outime[:, 1] > itime[1])
        tempindx = sp.where(log1 | log2 | log3)[0]
        time2files.append(filelisting[tempindx])

    curfilenum = -1
    for iparam, pname in enumerate(params):
        curparm = paramslower[iparam]
        # Use Ne from input to compare the ne derived from the power.
        if curparm == 'nepow':
            curparm = 'ne'
        datalist = []
        for itn, itime in enumerate(times):
            for iplot, filenum in enumerate(time2files[itn]):
                filenum = int(filenum)
                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 == 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(dataloc))

                for irngn, curcoord in enumerate(dataloc):

                    tempin = Ionoin.getclosestsphere(curcoord, [itime])[0]
                    Ntloc = tempin.shape[0]
                    tempin = sp.reshape(tempin, (Ntloc, len(pnameslowerin)))
                    curdata[irngn] = tempin[0, curprm]
                datalist.append(curdata)
        errordict[pname] = datadict[pname] - sp.hstack(datalist)
        errordictrel[pname] = 100. * errordict[pname] / sp.absolute(
            sp.hstack(datalist))
    return datadict, errordict, errordictrel, edatadict
Esempio n. 19
0
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)
Esempio n. 20
0
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