def main(testpath,npulse = 1400 ,functlist = ['spectrums','radardata','fitting','analysis']):
    """ 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 SimISR 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 SimISR to do.
    """
    
        
    curloc = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    
    
    
    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()

    
    configfilesetup(testpath,npulse)
    config = os.path.join(testpath,'stats.ini')
    (sensdict,simparams) = readconfigfile(config)
    makedata(testpath,simparams['Tint'])
    if check_run :
        runsim(functlist_red,testpath,config,True)
    if 'analysis' in functlist:
        analysisdump(testpath,config)
Example #2
0
def main(npulse=100, functlist=['spectrums', 'radardata', 'fitting', 'analysis'],radar='pfisr'):
    """ 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 SimISR 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 SimISR to do.
    """
    curloc = Path(__file__).resolve()
    testpath = curloc.parent.parent/'Testdata'/'BasicTest'
    if not testpath.is_dir():
        testpath.mkdir(parents=True)

    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()

    config = testpath.joinpath('stats.yml')
    if not config.exists():
        configfilesetup(str(testpath), npulse, radar)

    (_, simparams) = readconfigfile(str(config))
    makedata(testpath, simparams['Tint'])
    if check_run:
        runsim(functlist_red, str(testpath), config, True)
    if 'analysis' in functlist:
        analysisdump(str(testpath), config)
Example #3
0
def main(npulse=100,
         functlist=['spectrums', 'radardata', 'fitting', 'analysis']):
    """ 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 SimISR 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 SimISR to do.
    """

    curloc = Path(__file__).resolve()
    testpath = curloc.parent.parent / 'Testdata' / 'BasicTest'
    if not testpath.is_dir():
        testpath.mkdir(parents=True)

    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()

    configfilesetup(str(testpath), npulse)
    config = testpath.joinpath('stats.ini')
    (sensdict, simparams) = readconfigfile(str(config))
    makedata(testpath, simparams['Tint'])
    if check_run:
        runsim(functlist_red, str(testpath), config, True)
    if 'analysis' in functlist:
        analysisdump(str(testpath), config)
Example #4
0
    def __init__(self,ionoin,configfile,timein=None,mattype='matrix'):
        """ This will create the RadarSpaceTimeOperator object.
            Inputs
                ionoin - The input ionocontainer. This can be either an string that is a ionocontainer file,
                    a list of ionocontainer objects or a list a strings to ionocontainer files.
                config  - The ini file that used to set up the simulation.
                timein - A Ntx2 numpy array of times.
                RSTOPinv - The inverse operator object.
                invmat - The inverse matrix to the original operator.
        """
        mattype=mattype.lower()
        accepttypes=['matrix','sim','real']
        if not mattype in accepttypes:
            raise ValueError('Matrix type can only be {0}'.format(', '.join(accepttypes)))
        d2r = np.pi/180.0
        (sensdict,simparams) = readconfigfile(configfile)
        # determine if the input ionocontainer is a string, a list of strings or a list of ionocontainers.
        ionoin=makeionocombined(ionoin)
        #Input location
        self.Cart_Coords_In = ionoin.Cart_Coords
        self.Sphere_Coords_In = ionoin.Sphere_Coords

        # Set the input times
        if timein is None:
            self.Time_In = ionoin.Time_Vector
        else:
            self.Time_In = timein

        #Create an array of output location based off of the inputs
        rng_vec2 = simparams['Rangegatesfinal']
        nrgout = len(rng_vec2)

        angles = simparams['angles']
        nang =len(angles)

        ang_data = np.array([[iout[0],iout[1]] for iout in angles])
        rng_all = np.repeat(rng_vec2,(nang),axis=0)
        ang_all = np.tile(ang_data,(nrgout,1))
        self.Sphere_Coords_Out = np.column_stack((rng_all,ang_all))
        (R_vec,Az_vec,El_vec) = (self.Sphere_Coords_Out[:,0],self.Sphere_Coords_Out[:,1],
            self.Sphere_Coords_Out[:,2])
        xvecmult = np.sin(Az_vec*d2r)*np.cos(El_vec*d2r)
        yvecmult = np.cos(Az_vec*d2r)*np.cos(El_vec*d2r)
        zvecmult = np.sin(El_vec*d2r)
        X_vec = R_vec*xvecmult
        Y_vec = R_vec*yvecmult
        Z_vec = R_vec*zvecmult

        self.Cart_Coords_Out = np.column_stack((X_vec,Y_vec,Z_vec))
        self.Time_Out = np.column_stack((simparams['Timevec'],simparams['Timevec']+simparams['Tint']))+self.Time_In[0,0]
        self.simparams=simparams
        self.sensdict=sensdict
        self.lagmat = self.simparams['amb_dict']['WttMatrix']
        self.mattype=mattype
        # create the matrix
        (self.RSTMat,self.overlaps,self.blocklocs) = makematPA(ionoin.Sphere_Coords,ionoin.Cart_Coords,ionoin.Time_Vector,configfile,ionoin.Velocity,mattype)
Example #5
0
    def __init__(self, ionoacf, Ionosig=None, inifile='default.ini'):
        """ The init function for the fitter take the inputs for the fitter programs.

            Inputs:
            ionoacf: The lags in ionocontainer format.
            Ionosig:
            sensdict: The dictionary that holds the sensor info.
            simparams: The dictionary that hold the specific simulation params"""
        (self.sensdict, self.simparams) = readconfigfile(inifile)
        self.acf = ionoacf
        self.sig = Ionosig
Example #6
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,Tk.END)
                     self.paramdic[i][1].delete(0,Tk.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,Tk.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,Tk.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,Tk.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."
def plotparams(datapath,indch):
    # read in the files
    geofile = os.path.join(datapath,'Fitted','fitteddataGEOD.h5')
    acffile = os.path.join(datapath,'ACF','00lags.h5')
    geod = GeoData.read_h5(geofile)
    acfIono = IonoContainer.readh5(acffile)
    picklefile = os.path.join(datapath,'config.ini')
    (sensdict,simparams) = readconfigfile(picklefile)
    #determine the ind
    dataloc = geod.dataloc
    rngloc = 210
    ind = sp.argmin(sp.absolute(dataloc[:,0]-rngloc))
    #
    Ne = geod.data['Ne'][ind]
    Ti = geod.data['Ti'][ind]
    Te = geod.data['Te'][ind]


    #make a spectrum
    npts = 128
    datablock = sp.array([[Ne[indch],Ti[indch]],[Ne[indch],Te[indch]]])
    specobj = ISRSpectrum(centerFrequency =sensdict['fc'],nspec = npts,sampfreq=sensdict['fs'])
    (omeg,specexample,rcs) = specobj.getspecsep(datablock,simparams['species'],rcsflag=True)
    specexample = rcs*npts*specexample/sp.absolute(specexample).sum()
    acf = acfIono.Param_List[ind,indch]
    origspec = scfft.fftshift(scfft.fft(acf,n=npts)).real
    (figmplf, [[ax1,ax2],[ax3,ax4]]) = plt.subplots(2, 2,figsize=(16, 12), facecolor='w')


    ax1.plot(sp.arange(len(Ne)),Ne)
    ax1.set_title('$N_e$')
    ax2.plot(sp.arange(len(Ti)),Ti)
    ax2.set_title('$T_i$')
    ax3.plot(sp.arange(len(Te)),Te)
    ax3.set_title('$T_e$');
    spec1 = ax4.plot(omeg*1e-3,origspec,label='Measured')
    spec2 = ax4.plot(omeg*1e-3,specexample,label='Fitted')
    ax4.set_title('Measured and Fitted Spectrums')
    ax4.set_xlabel('Frequency KHz')
    ax4.legend(loc = 1)

#    Nevals = sp.linspace(5e10,5e11,10)
#    Tivals = sp.linspace(1e3,2e5,10)
#    Tevals = sp.linspace(1e3,2e5,10)
#    xlist = [[1],Tivals,Nevals,Tevals,[0]]
#    outsurf = makefitsurf(xlist,acf,sensdict,simparams)
    return(figmplf)
Example #8
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 SimISR 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 SimISR 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 = Path(__file__).resolve().parent
    testpath=curloc.parent.joinpath('Testdata','StatsTest')
    
    testpath.mkdir(exist_ok=True,parents=True)
    
    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 = testpath.joinpath(foldname)
        allfolds.append(curfold)
        
        curfold.mkdir(exist_ok=True,parents=True)
            
        configfilesetup(curfold,ip)
        makedata(curfold)
        config = 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,str(curfold.joinpath('stats.ini')),True)
        if 'analysis' in functlist:
            analysisdump(curfold,config)
        if 'stats' in functlist:
            makehist(curfold,ip)
Example #9
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 = Path(__file__).resolve().parent
    defcon = curloc.joinpath('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(testpath.joinpath('stats.ini'),simparams['Beamlist'],sensdict['Name'],simparams)
Example #10
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 = Path(__file__).resolve().parent
    defcon = curloc.joinpath('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(testpath.joinpath('stats.ini'),simparams['Beamlist'],sensdict['Name'],simparams)
def configfilesetup(testpath,npulses = 1400):
    """ 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'] = 3*tint
    
    simparams['startfile']='startfile.h5'
    makeconfigfile(os.path.join(testpath,'stats.ini'),simparams['Beamlist'],sensdict['Name'],simparams)
Example #12
0
def configfilesetup(testpath, npulses, radar='PFISR'):
    """ 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.
    """
    testpath = Path(testpath).expanduser()
    curloc = Path(__file__).resolve().parent
    if radar.lower() == 'pfisr':
        defcon = curloc / 'statsbase.ini'
    elif radar.lower() == 'millstonez':
        defcon = curloc / 'statsbasemhz.yml'
    (sensdict, simparams) = readconfigfile(defcon)
    tint = simparams['IPP'] * npulses
    ratio1 = tint / simparams['Tint']
    simparams['Tint'] = ratio1 * simparams['Tint']
    simparams['Fitinter'] = ratio1 * simparams['Fitinter']
    simparams['TimeLim'] = 2 * tint
    simparams['fitmode'] = 1
    simparams['startfile'] = 'startfile.h5'
    makeconfigfile(str(testpath / 'stats.yml'), simparams['Beamlist'],
                   sensdict['Name'], simparams)
Example #13
0
def makespectrums(basedir, configfile, printlines=True):
    """ 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.
         """
    basedir = Path(basedir).expanduser()
    dirio = ('Origparams', 'Spectrums')
    inputdir = basedir/dirio[0]
    outputdir = basedir/dirio[1]
    # determine the list of h5 files in Origparams directory
    dirlist = sorted(inputdir.glob('*.h5'))
    # Make the lists of numbers and file names for the dictionary
    (listorder, _, _, timebeg, _) = IonoContainer.gettimes(dirlist)
    slist = [dirlist[ikey] for ikey in listorder]
    (sensdict, simparams) = readconfigfile(configfile)
    # Delete data
    outfiles = outputdir.glob('*.h5')
    for ifile in outfiles:
        ifile.unlink()

    for inum, curfile in zip(timebeg, slist):

        outfile = outputdir / (str(inum)+' spectrum.h5')
        update_progress(float(inum)/float(len(slist)),
                        'Processing file {} starting at {}'.format(curfile.name, datetime.now()))
        curiono = IonoContainer.readh5(str(curfile))

        curiono.makespectruminstanceopen(specfuncs.ISRSspecmake, sensdict,
                                         int(simparams['numpoints']), float(inum),
                                         float(len(slist)), printlines).saveh5(str(outfile))
        update_progress(float(inum+1)/float(len(slist)),
                        'Finished file {} starting at {}'.format(curfile.name, datetime.now()))
Example #14
0
def configfilesetup(testpath, npulses, radar='PFISR'):
    """ 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.
    """
    testpath = Path(testpath).expanduser()
    curloc = Path(__file__).resolve().parent
    if radar.lower() == 'pfisr':
        defcon = curloc/'statsbase.ini'
    elif radar.lower() == 'millstonez':
        defcon = curloc/'statsbasemhz.yml'
    (sensdict, simparams) = readconfigfile(defcon)
    tint = simparams['IPP']*npulses
    ratio1 = tint/simparams['Tint']
    simparams['Tint'] = ratio1*simparams['Tint']
    simparams['Fitinter'] = ratio1 * simparams['Fitinter']
    simparams['TimeLim'] = 2*tint
    simparams['fitmode'] = 1
    simparams['startfile'] = 'startfile.h5'
    makeconfigfile(str(testpath/'stats.yml'), simparams['Beamlist'],
                   sensdict['Name'], simparams)
Example #15
0
    def __init__(self, Ionodict, inifile, outdir, outfilelist=None):
        """
            This function will create an instance of the RadarData class.  It will
            take in the values and create the class and make raw IQ data.
            Inputs:
            sensdict - A dictionary of sensor parameters
            angles - A list of tuples which the first position is the az angle
                and the second position is the el angle.
            IPP - The interpulse period in seconds represented as a float.
            Tint - The integration time in seconds as a float.  This will be the
            integration time of all of the beams.
            time_lim - The length of time of the simulation the number of time points
                will be calculated.
            pulse - A numpy array that represents the pulse shape.
            rng_lims - A numpy array of length 2 that holds the min and max range
                that the radar will cover.
        """
        (sensdict, simparams) = readconfigfile(inifile)
        self.simparams = simparams
        N_angles = len(self.simparams['angles'])

        NNs = int(self.simparams['NNs'])
        self.sensdict = sensdict
        Npall = sp.floor(self.simparams['TimeLim'] / self.simparams['IPP'])
        Npall = int(sp.floor(Npall / N_angles) * N_angles)
        Np = Npall / N_angles

        print("All spectrums created already")
        filetimes = Ionodict.keys()
        filetimes.sort()
        ftimes = sp.array(filetimes)
        simdtype = self.simparams['dtype']
        pulsetimes = sp.arange(Npall) * self.simparams['IPP'] + ftimes.min()
        pulsefile = sp.array(
            [sp.where(itimes - ftimes >= 0)[0][-1] for itimes in pulsetimes])
        # differentiate between phased arrays and dish antennas
        if sensdict['Name'].lower() in ['risr', 'pfisr', 'risr-n']:

            beams = sp.tile(sp.arange(N_angles), Npall / N_angles)
        else:

            # for dish arrays
            brate = simparams['beamrate']
            beams2 = sp.repeat(sp.arange(N_angles), brate)
            beam3 = sp.concatenate((beams2, beams2[::-1]))
            ntile = int(sp.ceil(Npall / len(beam3)))
            leftover = int(Npall - ntile * len(beam3))
            if ntile > 0:
                beams = sp.tile(beam3, ntile)
                beams = sp.concatenate((beams, beam3[:leftover]))
            else:
                beams = beam3[:leftover]

        pulsen = sp.repeat(sp.arange(Np), N_angles)
        pt_list = []
        pb_list = []
        pn_list = []
        fname_list = []
        self.datadir = outdir
        self.maindir = outdir.parent
        self.procdir = self.maindir / 'ACF'
        Nf = len(filetimes)
        progstr = 'Data from {:d} of {:d} being processed Name: {:s}.'
        if outfilelist is None:
            print('\nData Now being created.')

            Noisepwr = v_Boltz * sensdict['Tsys'] * sensdict['BandWidth']
            self.outfilelist = []
            for ifn, ifilet in enumerate(filetimes):

                outdict = {}
                ifile = Ionodict[ifilet]
                ifilename = Path(ifile).name
                update_progress(
                    float(ifn) / Nf, progstr.format(ifn, Nf, ifilename))
                curcontainer = IonoContainer.readh5(ifile)
                if ifn == 0:
                    self.timeoffset = curcontainer.Time_Vector[0, 0]
                pnts = pulsefile == ifn
                pt = pulsetimes[pnts]
                pb = beams[pnts]
                pn = pulsen[pnts].astype(int)
                rawdata = self.__makeTime__(pt, curcontainer.Time_Vector,
                                            curcontainer.Sphere_Coords,
                                            curcontainer.Param_List, pb)
                d_shape = rawdata.shape
                n_tempr = sp.random.randn(*d_shape).astype(simdtype)
                n_tempi = 1j * sp.random.randn(*d_shape).astype(simdtype)
                noise = sp.sqrt(Noisepwr / 2) * (n_tempr + n_tempi)
                outdict['AddedNoise'] = noise
                outdict['RawData'] = rawdata + noise
                outdict['RawDatanonoise'] = rawdata
                outdict['NoiseData'] = sp.sqrt(Noisepwr / 2) * (
                    sp.random.randn(len(pn), NNs).astype(simdtype) +
                    1j * sp.random.randn(len(pn), NNs).astype(simdtype))
                outdict['Pulses'] = pn
                outdict['Beams'] = pb
                outdict['Time'] = pt
                fname = '{0:d} RawData.h5'.format(ifn)
                newfn = self.datadir / fname
                self.outfilelist.append(str(newfn))
                dict2h5(str(newfn), outdict)

                #Listing info
                pt_list.append(pt)
                pb_list.append(pb)
                pn_list.append(pn)
                fname_list.append(fname)
            infodict = {
                'Files': fname_list,
                'Time': pt_list,
                'Beams': pb_list,
                'Pulses': pn_list
            }
            dict2h5(str(outdir.joinpath('INFO.h5')), infodict)

        else:
            infodict = h52dict(str(outdir.joinpath('INFO.h5')))
            alltime = sp.hstack(infodict['Time'])
            self.timeoffset = alltime.min()
            self.outfilelist = outfilelist
Example #16
0
def makematPA(Sphere_Coords,Cart_Coords,timein,configfile,vel=None,mattype='matrix'):
    """Make a Ntimeout*Nbeam*Nrng x Ntime*Nloc sparse matrix for the space time operator.
       The output space will have range repeated first, then beams then time.
       The coordinates will be [t0,b0,r0],[t0,b0,r1],[t0,b0,r2],...
       [t0,b1,r0],[t0,b1,r1], ... [t1,b0,r0],[t1,b0,r1],...[t1,b1,r0]...
       Inputs
           Sphere_Coords - A Nlocx3 array of the spherical coordinates of the input data.
           timein - A Ntbegx2 numpy array with the start and stop times of the input data.
           configfile - The ini file used for the simulation configuration.
           vel - A NlocxNtx3 numpy array of velocity.
       Outputs
           outmat - A list of matricies or a single matrix that is the forward between physical space
               to the discrete samples space of the radar.
           blocks - A tuple that holds the number of block matricies in overall forward operator.
           blocksize - A tuple that holds the shape of the outmatrix size.
           blockloc - An Ntout x Ntbeg array that holds the corresponding spatial forward model.
    """
    #
    (sensdict,simparams) = readconfigfile(configfile)
    timeout = simparams['Timevec']
    Tint = simparams['Tint']
    timeout = np.column_stack((timeout,timeout+Tint)) +timein[0,0]

    rng_bin=sensdict['t_s']*v_C_0*1e-3/2.

    angles = simparams['angles']
    Nbeams = len(angles)


    rng_vec2 = simparams['Rangegatesfinal']
    nrgout = len(rng_vec2)
    pulse=simparams['Pulse']
    p_samps = pulse.shape[0]
    rng_len=p_samps*rng_bin
    Nlocbeg = Cart_Coords.shape[0]
    Nlocout= Nbeams*nrgout
    Ntbeg = len(timein)
    Ntout = len(timeout)
    if vel is None:
        vel=np.zeros((Nlocbeg,Ntbeg,3))




    # determine the overlaps
    # change

    blockloc_in = [[i*Nlocbeg,(i+1)*Nlocbeg] for i in range(Ntout)]
    blockloc_out = [[i*Nlocout,(i+1)*Nlocout] for i in range(Ntout)]
    blockloc = [blockloc_in,blockloc_out]
    # set up blocks
    blocksize = (Ntout*Nbeams*nrgout,Nlocbeg*Ntout)
    # make the matrix
    outmat = np.sparse.lil_matrix(blocksize,dtype =np.float64)
    overlaps={}
    if mattype.lower()=='real':
        cor_ratio=.5
    else:
        cor_ratio=1.
    for iton,ito in enumerate(timeout):
        overlaps[iton]=[]
        firstone=True
        for ix, x in enumerate(timein):
            if ( x[0]+1<ito[1] or ix==(Ntbeg-1)) and x[1]-1>ito[0]:

                # find the end of the data
                if ix ==Ntbeg-1:
                    enp=ito[1]
                else:
                    enp = np.minimum(ito[1],x[1])
                stp = np.maximum(x[0],ito[0])

                curvel=vel[:,0]*1e-3
                # XXX This is just a quick fix
                curvel[:,-1]=0
                if firstone:
                    firstone=False
                    t_0 = stp.copy()
                    curdiff=np.zeros_like(vel[:,ix])
                    curdiff2=curdiff+float(enp-stp)*curvel
                else:
                 #   T_1=float(x[0]-t_0)
                    t_0=stp.copy()
                    curdiff= curdiff2
                    curdiff2=curdiff+float(enp-stp)*curvel
                #find amount of time for overlap
                ratio = float(enp-stp)/Tint
                # set up new coordinate system
                # The thee types of coordinates are as follows
                # The matrix type assumes that the matrix will be applied to the data.
                # The sim type
                if mattype.lower()=='matrix':
                    newcoorsds1 = cart2sphere(Cart_Coords)
                    newcoorsds2 = cart2sphere(Cart_Coords)
                elif mattype.lower=='real':
                    newcoorsds1 = cart2sphere(Cart_Coords+curdiff)
                    newcoorsds2 = cart2sphere(Cart_Coords+curdiff2)
                else:
                    newcoorsds1 = cart2sphere(Cart_Coords+curdiff)
                    newcoorsds2 = cart2sphere(Cart_Coords+curdiff)
                overlaps[iton].append([ix,ratio,newcoorsds1,newcoorsds2])
    # make the matrix
    for iton,ito in enumerate(timeout):
        cur_over = overlaps[iton]
#        if mattype=='matrix':
#            cur_over=[cur_over[0]]
#            cur_over[0][1]=1.
        beamnorm=np.ones(Nbeams*nrgout)
        for it_in,it_info in enumerate(cur_over):
            print('\t Making Input time {0:d} of {1:d}'.format(it_in,len(cur_over)))
            cur_it,cur_ratio,Sp1,Sp2 = it_info
#            if mattype.lower()=='sim':
#                cur_ratio=1.
            rho1 = Sp1[:,0]
            Az1 = Sp1[:,1]
            El1 = Sp1[:,2]
            rho2 = Sp2[:,0]
            Az2 = Sp2[:,1]
            El2 = Sp2[:,2]


            # get the weights
            weights1 = {ibn:sensdict['ArrayFunc'](Az1,El1,ib[0],ib[1],sensdict['Angleoffset']) for ibn, ib in enumerate(angles)}
            weights2 = {ibn:sensdict['ArrayFunc'](Az2,El2,ib[0],ib[1],sensdict['Angleoffset']) for ibn, ib in enumerate(angles)}

            for ibn in range(Nbeams):


                print('\t\t Making Beam {0:d} of {1:d}'.format(ibn,Nbeams))
                weight1 = weights1[ibn]
                weight2 = weights2[ibn]
                for isamp in range(nrgout):
                    # make the row
                    irow = ibn + isamp*Nbeams + Nbeams*nrgout*iton
                    range_g = rng_vec2[isamp]
                    rnglims = [range_g-rng_len/2.,range_g+rng_len/2.]

                    # assume centered lag product.
                    rangelog = ((rho1>=rnglims[0])&(rho1<rnglims[1]))
                    # This is a nearest neighbors interpolation for the spectrums in the range domain
                    if np.sum(rangelog)==0:
                        minrng = np.argmin(np.absolute(range_g-rho1))
                        rangelog[minrng] = True
                    #create the weights and weight location based on the beams pattern.
                    weight_cur = weight1[rangelog]
                    # fix averaging problems
                    if it_in==0:
                        beamnorm[ibn + isamp*Nbeams]=weight_cur.sum()
                    weight_cur = weight_cur/beamnorm[ibn + isamp*Nbeams]
                    icols = np.where(rangelog)[0] + Nlocbeg*iton
#                    icols = np.where(rangelog)[0] + Nlocbeg*cur_it

                    weights_final = weight_cur*range_g**2/rho1[rangelog]**2
                    outmat[irow,icols] = weights_final*cur_ratio*cor_ratio+outmat[irow,icols]

                    if mattype.lower()=='real':
                        # assume centered lag product.
                        rangelog = ((rho2>=rnglims[0])&(rho2<rnglims[1]))
                        # This is a nearest neighbors interpolation for the spectrums in the range domain
                        if np.sum(rangelog)==0:
                            minrng = np.argmin(np.absolute(range_g-rho2))
                            rangelog[minrng] = True
                        #create the weights and weight location based on the beams pattern.
                        weight_cur =weight2[rangelog]
                        weight_cur = weight_cur//beamnorm[ibn + isamp*Nbeams]
    #                    icols = np.where(rangelog)[0]+ Nlocbeg*cur_it
                        icols = np.where(rangelog)[0]+ Nlocbeg*iton
                        weights_final = weight_cur*range_g**2/rho2[rangelog]**2
                        outmat[irow,icols] = weights_final*cur_ratio*cor_ratio+outmat[irow,icols]





    return(outmat,overlaps,blockloc)
Example #17
0
def makematPA(Sphere_Coords,
              Cart_Coords,
              timein,
              configfile,
              vel=None,
              mattype='matrix'):
    """Make a Ntimeout*Nbeam*Nrng x Ntime*Nloc sparse matrix for the space time operator.
       The output space will have range repeated first, then beams then time.
       The coordinates will be [t0,b0,r0],[t0,b0,r1],[t0,b0,r2],...
       [t0,b1,r0],[t0,b1,r1], ... [t1,b0,r0],[t1,b0,r1],...[t1,b1,r0]...
       Inputs
           Sphere_Coords - A Nlocx3 array of the spherical coordinates of the input data.
           timein - A Ntbegx2 numpy array with the start and stop times of the input data.
           configfile - The ini file used for the simulation configuration.
           vel - A NlocxNtx3 numpy array of velocity.
       Outputs
           outmat - A list of matricies or a single matrix that is the forward between physical space
               to the discrete samples space of the radar.
           blocks - A tuple that holds the number of block matricies in overall forward operator.
           blocksize - A tuple that holds the shape of the outmatrix size.
           blockloc - An Ntout x Ntbeg array that holds the corresponding spatial forward model.
    """
    #
    (sensdict, simparams) = readconfigfile(configfile)
    timeout = simparams['Timevec']
    Tint = simparams['Tint']
    timeout = np.column_stack((timeout, timeout + Tint)) + timein[0, 0]

    rng_bin = sensdict['t_s'] * v_C_0 * 1e-3 / 2.

    angles = simparams['angles']
    Nbeams = len(angles)

    rng_vec2 = simparams['Rangegatesfinal']
    nrgout = len(rng_vec2)
    pulse = simparams['Pulse']
    p_samps = pulse.shape[0]
    rng_len = p_samps * rng_bin
    Nlocbeg = Cart_Coords.shape[0]
    Nlocout = Nbeams * nrgout
    Ntbeg = len(timein)
    Ntout = len(timeout)
    if vel is None:
        vel = np.zeros((Nlocbeg, Ntbeg, 3))

    # determine the overlaps
    # change

    blockloc_in = [[i * Nlocbeg, (i + 1) * Nlocbeg] for i in range(Ntout)]
    blockloc_out = [[i * Nlocout, (i + 1) * Nlocout] for i in range(Ntout)]
    blockloc = [blockloc_in, blockloc_out]
    # set up blocks
    blocksize = (Ntout * Nbeams * nrgout, Nlocbeg * Ntout)
    # make the matrix
    outmat = np.sparse.lil_matrix(blocksize, dtype=np.float64)
    overlaps = {}
    if mattype.lower() == 'real':
        cor_ratio = .5
    else:
        cor_ratio = 1.
    for iton, ito in enumerate(timeout):
        overlaps[iton] = []
        firstone = True
        for ix, x in enumerate(timein):
            if (x[0] + 1 < ito[1] or ix == (Ntbeg - 1)) and x[1] - 1 > ito[0]:

                # find the end of the data
                if ix == Ntbeg - 1:
                    enp = ito[1]
                else:
                    enp = np.minimum(ito[1], x[1])
                stp = np.maximum(x[0], ito[0])

                curvel = vel[:, 0] * 1e-3
                # XXX This is just a quick fix
                curvel[:, -1] = 0
                if firstone:
                    firstone = False
                    t_0 = stp.copy()
                    curdiff = np.zeros_like(vel[:, ix])
                    curdiff2 = curdiff + float(enp - stp) * curvel
                else:
                    #   T_1=float(x[0]-t_0)
                    t_0 = stp.copy()
                    curdiff = curdiff2
                    curdiff2 = curdiff + float(enp - stp) * curvel
                #find amount of time for overlap
                ratio = float(enp - stp) / Tint
                # set up new coordinate system
                # The thee types of coordinates are as follows
                # The matrix type assumes that the matrix will be applied to the data.
                # The sim type
                if mattype.lower() == 'matrix':
                    newcoorsds1 = cart2sphere(Cart_Coords)
                    newcoorsds2 = cart2sphere(Cart_Coords)
                elif mattype.lower == 'real':
                    newcoorsds1 = cart2sphere(Cart_Coords + curdiff)
                    newcoorsds2 = cart2sphere(Cart_Coords + curdiff2)
                else:
                    newcoorsds1 = cart2sphere(Cart_Coords + curdiff)
                    newcoorsds2 = cart2sphere(Cart_Coords + curdiff)
                overlaps[iton].append([ix, ratio, newcoorsds1, newcoorsds2])
    # make the matrix
    for iton, ito in enumerate(timeout):
        cur_over = overlaps[iton]
        #        if mattype=='matrix':
        #            cur_over=[cur_over[0]]
        #            cur_over[0][1]=1.
        beamnorm = np.ones(Nbeams * nrgout)
        for it_in, it_info in enumerate(cur_over):
            print('\t Making Input time {0:d} of {1:d}'.format(
                it_in, len(cur_over)))
            cur_it, cur_ratio, Sp1, Sp2 = it_info
            #            if mattype.lower()=='sim':
            #                cur_ratio=1.
            rho1 = Sp1[:, 0]
            Az1 = Sp1[:, 1]
            El1 = Sp1[:, 2]
            rho2 = Sp2[:, 0]
            Az2 = Sp2[:, 1]
            El2 = Sp2[:, 2]

            # get the weights
            weights1 = {
                ibn: sensdict['ArrayFunc'](Az1, El1, ib[0], ib[1],
                                           sensdict['Angleoffset'])
                for ibn, ib in enumerate(angles)
            }
            weights2 = {
                ibn: sensdict['ArrayFunc'](Az2, El2, ib[0], ib[1],
                                           sensdict['Angleoffset'])
                for ibn, ib in enumerate(angles)
            }

            for ibn in range(Nbeams):

                print('\t\t Making Beam {0:d} of {1:d}'.format(ibn, Nbeams))
                weight1 = weights1[ibn]
                weight2 = weights2[ibn]
                for isamp in range(nrgout):
                    # make the row
                    irow = ibn + isamp * Nbeams + Nbeams * nrgout * iton
                    range_g = rng_vec2[isamp]
                    rnglims = [range_g - rng_len / 2., range_g + rng_len / 2.]

                    # assume centered lag product.
                    rangelog = ((rho1 >= rnglims[0]) & (rho1 < rnglims[1]))
                    # This is a nearest neighbors interpolation for the spectrums in the range domain
                    if np.sum(rangelog) == 0:
                        minrng = np.argmin(np.absolute(range_g - rho1))
                        rangelog[minrng] = True
                    #create the weights and weight location based on the beams pattern.
                    weight_cur = weight1[rangelog]
                    # fix averaging problems
                    if it_in == 0:
                        beamnorm[ibn + isamp * Nbeams] = weight_cur.sum()
                    weight_cur = weight_cur / beamnorm[ibn + isamp * Nbeams]
                    icols = np.where(rangelog)[0] + Nlocbeg * iton
                    #                    icols = np.where(rangelog)[0] + Nlocbeg*cur_it

                    weights_final = weight_cur * range_g**2 / rho1[rangelog]**2
                    outmat[
                        irow,
                        icols] = weights_final * cur_ratio * cor_ratio + outmat[
                            irow, icols]

                    if mattype.lower() == 'real':
                        # assume centered lag product.
                        rangelog = ((rho2 >= rnglims[0]) & (rho2 < rnglims[1]))
                        # This is a nearest neighbors interpolation for the spectrums in the range domain
                        if np.sum(rangelog) == 0:
                            minrng = np.argmin(np.absolute(range_g - rho2))
                            rangelog[minrng] = True
                        #create the weights and weight location based on the beams pattern.
                        weight_cur = weight2[rangelog]
                        weight_cur = weight_cur // beamnorm[ibn +
                                                            isamp * Nbeams]
                        #                    icols = np.where(rangelog)[0]+ Nlocbeg*cur_it
                        icols = np.where(rangelog)[0] + Nlocbeg * iton
                        weights_final = weight_cur * range_g**2 / rho2[
                            rangelog]**2
                        outmat[
                            irow,
                            icols] = weights_final * cur_ratio * cor_ratio + outmat[
                                irow, icols]

    return (outmat, overlaps, blockloc)
Example #18
0
def runfitter(paramdict, SNR, n_pulses, n_runs, Niratio, x_0=SVALS):
    """
        paramdict
    """

    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, 1e9]])
    vel = sp.zeros((nz, 1, 3))

    (sensdict, simparams) = readconfigfile(defcon)

    species = paramdict['species']
    nspecies = len(species)
    ni = nspecies-1
    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)

    omeg, outspecs = specfuncs.ISRSspecmake(Icont1, sensdict, int(simparams['numpoints']))

    tau, acf = spect2acf(omeg, outspecs)

    t_log = sp.logical_and(tau<simparams['Pulselength'],tau>=0)
    plen = t_log.sum()
    amb_dict = simparams['amb_dict']
    amb_mat_flat = sp.zeros_like(amb_dict['WttMatrix'])
    eyemat = sp.eye(plen)
    amb_mat_flat[:plen, t_log] = eyemat
    fitmode = simparams['fitmode']
    simparams['amb_dict']['WttMatrix'] = amb_mat_flat

    Nloc, Nt = acf.shape[:2]
    # output Files
    fittedarray = sp.zeros((Nloc, Nt, nparams+1))*sp.nan
    fittederror = sp.zeros((Nloc, Nt, nparams+1))*sp.nan
    fittedcov = sp.zeros((Nloc, Nt, 4, 4))*sp.nan
    funcevals = sp.zeros((Nloc, Nt))
    for iloc, ilocvec in acf:
        for itn, iacf in ilocvec:
            curlag = sp.dot(amb_mat_flat, iacf)
            std_lag = sp.absolute(curlag)(1.+1./SNR)/sp.sqrt(n_pulses)
            covar_lag = sp.diag(sp.power(std_lag,2.))

            curlag = curlag + std_lag*sp.random.randn(curlag.shape)
            d_func = (curlag, sensdict, simparams, Niratio)
            # Only fit Ti, Te, Ne and Vi

            # change variables because of new fit mode

            # Perform the fitting
            optresults = scipy.optimize.least_squares(fun=specfuncs.ISRSfitfunction, x0=x_0,
                                                      method='lm', verbose=0, args=d_func)
            x_res = optresults.x.real
            # Derive data for the ions using output from the fitter and ion
            # species ratios which are assumed to be given.
            ionstuff = sp.zeros(ni*2-1)
            ionstuff[:2*ni:2] = x_res[1]*Niratio
            ionstuff[1:2*ni-1:2] = x_res[0]
            # change variables because of new fit mode
            if fitmode == 1:
                x_res[2] = x_res[2]*x_res[0]
            fittedarray[iloc, itn] = sp.append(ionstuff,
                                               sp.append(x_res, Ne_start[iloc, itime]))
            funcevals[iloc, itn] = optresults.nfev
            #                fittedarray[iloc,itime] = sp.append(optresults.x,Ne_start[iloc,itime])
            resid = optresults.cost
            jac = optresults.jac
            # combine the rows because of the complex conjugates
            jacc = jac[0::2]+jac[1::2]
            try:
                # Derive covariances for the ions using output from the fitter and ion species ratios which are assumed to be given.
                #covf = sp.linalg.inv(sp.dot(jac.transpose(),jac))*resid/dof


                covf = sp.linalg.inv(sp.dot(sp.dot(jacc.transpose(),
                                                   sp.linalg.inv(covar_lag)), jacc))

                # change variables because of new fit mode
                if fitmode == 1:
                    # is this right?
                    covf[2] = covf[2]*x_res[0]
                    covf[:,2] = covf[:,2]*x_res[0]
                vars_vec = sp.diag(covf).real
                ionstuff = sp.zeros(ni*2-1)
                ionstuff[:2*ni:2] = vars_vec[1]*Niratio
                ionstuff[1:2*ni-1:2] = vars_vec[0]
                vars_vec = sp.append(ionstuff, vars_vec)
                fittedcov[iloc, itn] = covf
            except:#sp.linalg.LinAlgError('singular matrix'):
                vars_vec = sp.ones(nparams)*float('nan')

        #                if len(vars_vec)<fittederror.shape[-1]-1:
        #                    pdb.set_trace()
        fittederror[iloc, itn, :-1] = vars_vec



    return(fittedarray, fittederror, funcevals, fittedcov)
Example #19
0
    if basedir.lower() == 'all':
        basedirlist = glob.glob(os.path.join(curpath,'exp_width_*'))
    else:
        basedirlist = basedir.split()

    if 'paramsweep' in funcnamelist:
        
        parametersweep(basedir,configfile,acfdir=acffolder,invtype=invtype)
        funcnamelist.remove('paramsweep')
    if 'invertdata' in funcnamelist:
        runinversion(basedir,configfile,acfdir=acffolder,invtype=invtype)
        funcnamelist.remove('invertdata')
    if 'origdata' in funcnamelist:
        funcnamelist.remove('origdata')
        makedirs = True
        (sensdict,simparams) = readconfigfile(configfile)
        azangles = [iang[0] for iang in simparams['angles']]
        meanaz = sp.mean(azangles)
        for ibase in basedirlist:
            makeline(ibase,meanaz,linewidth=lw,multval = mult)

    if 'all' in funcnamelist:

        funcnamelist=['spectrums','applymat','fittingmat','plotting']

    

    plotboolin = False
    plotboolout= False
    ploterror=False
    plotmat=False
Example #20
0
def plotpercenterror(testdir,imgdir,config,wtimes=False,fitpath='Fitted',fitfile='fitteddata.h5'):
    """
    This will plot all of the fitted data with each time step as a pcolor images of
    electron density and electron density from power mesurements.
    Inputs
    testdir - The directory with the input data in h5 files formated for the ionocontainer structure.
    imgdir - The directory that holds the images.
    """
    (sensdict,simparams)=readconfigfile(config)
    tvec = simparams['Timevec']
    filetemplate = 'fitteddataerrorpercent'
    if os.path.exists(imgdir):
        imgfiles = glob.glob(os.path.join(imgdir,'*'+filetemplate+'.png'))
        for imgf in imgfiles:
            os.remove(imgf)
    else:
        os.mkdir(imgdir)
    filename = os.path.join(testdir,fitpath,fitfile)
    iono = IonoContainer.readh5(filename)
    Iono1 = GeoData(readIono,[iono])
    rngrdr =Iono1.dataloc[:,0].astype('float32')
    sign1 = sp.sign(Iono1.dataloc[:,1])
    el = Iono1.dataloc[:,2].astype('float32')
    elvec,elinv = sp.unique(el,return_inverse=True)
    nbeams = len(elvec)
    nrg = len(rngrdr)/nbeams
    nt = Iono1.times.shape[0]
    Rngrdrmat = sp.reshape(rngrdr,(nrg,nbeams))
    Signmat = sp.reshape(sign1,(nrg,nbeams))
    Elmat = sp.reshape(el,(nrg,nbeams))
    permin,permax=[0.,100.]
    Xmat = Rngrdrmat*Signmat*sp.cos(Elmat*sp.pi/180.)
    Zmat = Rngrdrmat*sp.sin(Elmat*sp.pi/180.)

    Ne = Iono1.data['Ne'].reshape(nrg,nbeams,nt)
    Ti = Iono1.data['Ti'].reshape(nrg,nbeams,nt)
    Te = Iono1.data['Te'].reshape(nrg,nbeams,nt)
    nNe = 100.*Iono1.data['nNe'].reshape(nrg,nbeams,nt)/Ne
    nTe = 100.*Iono1.data['nTe'].reshape(nrg,nbeams,nt)/Te
    nTi = 100.*Iono1.data['nTi'].reshape(nrg,nbeams,nt)/Ti
    

    imcount=0
    
    dsetname = os.path.split(os.path.dirname(testdir))[-1]
    print "Plotting Output error data for "+dsetname

    if 'perryplane' in testdir.lower():
        xlim = [-200.,360.]
        xticks = [-150.,0.,150.,300.]
        allparams=True
        ncols=3
        figsize = (15,7)
    else:
        xlim = [0.,400.]
        xticks = [0.,150.,300]
        allparams = False
        ncols=1
        figsize = (5,7)
    ylim = [100.,500]
    for itimen,itime in enumerate(Iono1.times):
        print "{0} Output for {1} of {2}".format(dsetname,itimen,len(Iono1.times))
        
        
        Nemat = nNe[:,:,itimen]
        Timat = nTi[:,:,itimen]
        Temat = nTe[:,:,itimen]
        
        fig ,axmat= plt.subplots(nrows=1,ncols=ncols,facecolor='w',figsize=figsize,sharey=True)
        if allparams:
            avec=axmat.flatten()
        else:
            avec=[axmat]

        plt.sca(avec[0])
        avec[0].set_xlabel('X Plane in km',fontsize=fs)
        avec[0].set_ylabel('Alt in km',fontsize=fs)
        pc1 = avec[0].pcolor(Xmat,Zmat,Nemat,cmap = defmap,vmin=permin,vmax=permax)
        plt.tick_params(labelsize=16)
        plt.xticks(xticks)
        avec[0].set_xlim(xlim)
        avec[0].set_ylim(ylim)
        avec[0].set_title('Electron Density % Error',fontsize=fs)
        tick_locator = ticker.MaxNLocator(nbins=5)
            
        cb1 = plt.colorbar(pc1, ax=avec[0])
        cb1.ax.xaxis.set_label_position('top')
        cb1.ax.set_xlabel(ercbarstr,fontsize=fscb)
        cb1.locator = tick_locator
        cb1.update_ticks()
        if allparams:

            
            plt.sca(avec[1])
            plt.tick_params(labelsize=16)
            plt.xticks(xticks)
            avec[1].set_xlabel('X Plane in km',fontsize=fs)
            pc2 = avec[1].pcolor(Xmat,Zmat,Temat,cmap = defmap,vmin=permin,vmax=permax)
            avec[1].set_xlim(xlim)
            avec[1].set_ylim(ylim)
            avec[1].set_title('Electron Temperature % Error',fontsize=fs)

            cb2 = plt.colorbar(pc2, ax=avec[1])
            cb2.ax.xaxis.set_label_position('top')
            cb2.ax.set_xlabel(ercbarstr,fontsize=fscb)
            cb2.locator = tick_locator
            cb2.update_ticks()
            plt.sca(avec[2])
            plt.xticks(xticks)
            plt.tick_params(labelsize=16)
            avec[2].set_xlabel('X Plane in km',fontsize=fs)
            pc3 = avec[2].pcolor(Xmat,Zmat,Timat,cmap = defmap,vmin=permin,vmax=permax)
            avec[2].set_xlim(xlim)
            avec[2].set_ylim(ylim)
            avec[2].set_title('Ion Temperature % Error',fontsize=fs)

            cb3 = plt.colorbar(pc3, ax=avec[2])
            cb3.ax.xaxis.set_label_position('top')
            cb3.ax.set_xlabel(ercbarstr,fontsize=fscb)
            cb3.locator = tick_locator
            cb3.update_ticks()	
        
        plt.tight_layout()
        if wtimes:
            plt.subplots_adjust(top=0.9)
            spti = fig.suptitle('Percent Error at {0} seconds'.format(int(tvec[itimen])),fontsize=24)
       

        fname= '{0:0>3}_'.format(imcount)+filetemplate+'.png'
        plt.savefig(os.path.join(imgdir,fname),dpi=300)
        imcount=imcount+1
        plt.close(fig)
Example #21
0
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)
Example #22
0
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)
Example #23
0
    def __init__(self, ionoin, configfile, timein=None, mattype='matrix'):
        """ This will create the RadarSpaceTimeOperator object.
            Inputs
                ionoin - The input ionocontainer. This can be either an string that is a ionocontainer file,
                    a list of ionocontainer objects or a list a strings to ionocontainer files.
                config  - The ini file that used to set up the simulation.
                timein - A Ntx2 numpy array of times.
                RSTOPinv - The inverse operator object.
                invmat - The inverse matrix to the original operator.
        """
        mattype = mattype.lower()
        accepttypes = ['matrix', 'sim', 'real']
        if not mattype in accepttypes:
            raise ValueError('Matrix type can only be {0}'.format(
                ', '.join(accepttypes)))
        d2r = np.pi / 180.0
        (sensdict, simparams) = readconfigfile(configfile)
        # determine if the input ionocontainer is a string, a list of strings or a list of ionocontainers.
        ionoin = makeionocombined(ionoin)
        #Input location
        self.Cart_Coords_In = ionoin.Cart_Coords
        self.Sphere_Coords_In = ionoin.Sphere_Coords

        # Set the input times
        if timein is None:
            self.Time_In = ionoin.Time_Vector
        else:
            self.Time_In = timein

        #Create an array of output location based off of the inputs
        rng_vec2 = simparams['Rangegatesfinal']
        nrgout = len(rng_vec2)

        angles = simparams['angles']
        nang = len(angles)

        ang_data = np.array([[iout[0], iout[1]] for iout in angles])
        rng_all = np.repeat(rng_vec2, (nang), axis=0)
        ang_all = np.tile(ang_data, (nrgout, 1))
        self.Sphere_Coords_Out = np.column_stack((rng_all, ang_all))
        (R_vec, Az_vec,
         El_vec) = (self.Sphere_Coords_Out[:, 0], self.Sphere_Coords_Out[:, 1],
                    self.Sphere_Coords_Out[:, 2])
        xvecmult = np.sin(Az_vec * d2r) * np.cos(El_vec * d2r)
        yvecmult = np.cos(Az_vec * d2r) * np.cos(El_vec * d2r)
        zvecmult = np.sin(El_vec * d2r)
        X_vec = R_vec * xvecmult
        Y_vec = R_vec * yvecmult
        Z_vec = R_vec * zvecmult

        self.Cart_Coords_Out = np.column_stack((X_vec, Y_vec, Z_vec))
        self.Time_Out = np.column_stack(
            (simparams['Timevec'],
             simparams['Timevec'] + simparams['Tint'])) + self.Time_In[0, 0]
        self.simparams = simparams
        self.sensdict = sensdict
        self.lagmat = self.simparams['amb_dict']['WttMatrix']
        self.mattype = mattype
        # create the matrix
        (self.RSTMat, self.overlaps,
         self.blocklocs) = makematPA(ionoin.Sphere_Coords, ionoin.Cart_Coords,
                                     ionoin.Time_Vector, configfile,
                                     ionoin.Velocity, mattype)
Example #24
0
    def __init__(self, Ionodict, inifile, outdir, outfilelist=None):
        """
            This function will create an instance of the RadarData class.  It will
            take in the values and create the class and make raw IQ data.
            Inputs:
            sensdict - A dictionary of sensor parameters
            angles - A list of tuples which the first position is the az angle
                and the second position is the el angle.
            IPP - The interpulse period in seconds represented as a float.
            Tint - The integration time in seconds as a float.  This will be the
            integration time of all of the beams.
            time_lim - The length of time of the simulation the number of time points
                will be calculated.
            pulse - A numpy array that represents the pulse shape.
            rng_lims - A numpy array of length 2 that holds the min and max range
                that the radar will cover.
        """
        (sensdict, simparams) = readconfigfile(inifile)
        self.simparams = simparams
        N_angles = len(self.simparams['angles'])

        NNs = int(self.simparams['NNs'])
        self.sensdict = sensdict
        Npall = sp.floor(self.simparams['TimeLim']/self.simparams['IPP'])
        Npall = int(sp.floor(Npall/N_angles)*N_angles)
        Np = Npall/N_angles

        print("All spectrums created already")
        filetimes = Ionodict.keys()
        filetimes.sort()
        ftimes = sp.array(filetimes)
        simdtype = self.simparams['dtype']
        pulsetimes = sp.arange(Npall)*self.simparams['IPP'] +ftimes.min()
        pulsefile = sp.array([sp.where(itimes-ftimes >= 0)[0][-1] for itimes in pulsetimes])
        # differentiate between phased arrays and dish antennas
        if sensdict['Name'].lower() in ['risr', 'pfisr', 'risr-n']:

            beams = sp.tile(sp.arange(N_angles), Npall/N_angles)
        else:

            # for dish arrays
            brate = simparams['beamrate']
            beams2 = sp.repeat(sp.arange(N_angles), brate)
            beam3 = sp.concatenate((beams2, beams2[::-1]))
            ntile = int(sp.ceil(Npall/len(beam3)))
            leftover = int(Npall-ntile*len(beam3))
            if ntile > 0:
                beams = sp.tile(beam3, ntile)
                beams = sp.concatenate((beams, beam3[:leftover]))
            else:
                beams = beam3[:leftover]

        pulsen = sp.repeat(sp.arange(Np), N_angles)
        pt_list = []
        pb_list = []
        pn_list = []
        fname_list = []
        self.datadir = outdir
        self.maindir = outdir.parent
        self.procdir = self.maindir/'ACF'
        Nf = len(filetimes)
        progstr = 'Data from {:d} of {:d} being processed Name: {:s}.'
        if outfilelist is None:
            print('\nData Now being created.')

            Noisepwr = v_Boltz*sensdict['Tsys']*sensdict['BandWidth']
            self.outfilelist = []
            for ifn, ifilet in enumerate(filetimes):

                outdict = {}
                ifile = Ionodict[ifilet]
                ifilename = Path(ifile).name
                update_progress(float(ifn)/Nf, progstr.format(ifn, Nf, ifilename))
                curcontainer = IonoContainer.readh5(ifile)
                if ifn == 0:
                    self.timeoffset = curcontainer.Time_Vector[0, 0]
                pnts = pulsefile == ifn
                pt = pulsetimes[pnts]
                pb = beams[pnts]
                pn = pulsen[pnts].astype(int)
                rawdata = self.__makeTime__(pt, curcontainer.Time_Vector,
                                            curcontainer.Sphere_Coords,
                                            curcontainer.Param_List, pb)
                d_shape = rawdata.shape
                n_tempr = sp.random.randn(*d_shape).astype(simdtype)
                n_tempi = 1j*sp.random.randn(*d_shape).astype(simdtype)
                noise = sp.sqrt(Noisepwr/2)*(n_tempr+n_tempi)
                outdict['AddedNoise'] = noise
                outdict['RawData'] = rawdata+noise
                outdict['RawDatanonoise'] = rawdata
                outdict['NoiseData'] = sp.sqrt(Noisepwr/2)*(sp.random.randn(len(pn), NNs).astype(simdtype)+
                                                            1j*sp.random.randn(len(pn), NNs).astype(simdtype))
                outdict['Pulses'] = pn
                outdict['Beams'] = pb
                outdict['Time'] = pt
                fname = '{0:d} RawData.h5'.format(ifn)
                newfn = self.datadir/fname
                self.outfilelist.append(str(newfn))
                dict2h5(str(newfn), outdict)

                #Listing info
                pt_list.append(pt)
                pb_list.append(pb)
                pn_list.append(pn)
                fname_list.append(fname)
            infodict = {'Files':fname_list, 'Time':pt_list, 'Beams':pb_list, 'Pulses':pn_list}
            dict2h5(str(outdir.joinpath('INFO.h5')), infodict)

        else:
            infodict = h52dict(str(outdir.joinpath('INFO.h5')))
            alltime = sp.hstack(infodict['Time'])
            self.timeoffset = alltime.min()
            self.outfilelist = outfilelist
Example #25
0
def main():

    (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)