Exemple #1
0
    def savefile(self):
        """Saves the parameters out"""
        fn = tkFileDialog.asksaveasfilename(title="Save File",filetypes=[('INI','.ini'),('PICKLE','.pickle')])
        blist = self.pickbeams.output
        self.pickbeams.buttonClick(fn)
        radarname = self.pickbeams.var.get()
        posspec =  ['N2+', 'N+', 'O+', 'NO+', 'H+', 'O2+','e-' ]
        specieslist = self.species.get().lower().split()
        newlist =[x for x in posspec if x.lower() in specieslist]

        if 'e-' not in newlist:newlist.append('e-')
        
        
        simparams ={'IPP':float(self.ipp.get()),
                    'TimeLim':float(self.timelim.get()),
                    'RangeLims':[int(float(self.rangelimlow.get())),int(float(self.rangelimhigh.get()))],
                    'Pulselength':1e-6*float(self.pulselength.get()),
                    't_s': 1e-6*float(self.t_s.get()),
                    'Pulsetype':self.pulsetype.get(),
                    'Tint':float(self.tint.get()),
                    'Fitinter':float(self.fitinter.get()),
                    'NNs': int(float(self.nns.get())),
                    'NNp':int(float(self.nnp.get())),
                    'dtype':{'complex128':sp.complex128,'complex64':sp.complex64}[self.dtype.get()],
                    'ambupsamp':int(float(self.ambupsamp.get())),
                    'species':newlist,
                    'numpoints':int(float(self.numpoints.get())),
                    'startfile':self.startfile.get(),
                    'FitType': self.fittype.get()}
        
        if len(self.outangles.get())>0:
            outlist1 = self.outangles.get().split(',')
            simparams['outangles']=[[ float(j) for j in  i.lstrip().rstrip().split(' ')] for i in outlist1]
                    
        makeconfigfile(fn,blist,radarname,simparams)
Exemple #2
0
def makedatasets(maindir='~/DATA/Ion_Comp_Exp/',
                 nomult=sp.arange(1, 5, dtype=float),
                 baud_len=[7, 14, 21]):
    """
        This will make all of the data sets.
    """
    basepath = Path(maindir).expanduser()
    basepath.mkdir(exist_ok=True)
    iono_orig = pyglowinput()
    nion = len(iono_orig.Species) - 1
    npulses = 1000
    (sensdict, simparams) = getdefualtparams()
    tint = simparams['IPP'] * npulses
    ratio1 = tint / simparams['Tint']
    simparams['RangeLims'] = [80., 750.]
    simparams['Tint'] = ratio1 * simparams['Tint']
    simparams['Fitinter'] = ratio1 * simparams['Fitinter']
    simparams['TimeLim'] = 12 * tint
    ts = simparams['t_s']
    simparams['beamrate'] = 1
    simparams['outangles'] = [[0]]
    simparams['species'] = ['O+', 'NO+', 'O2+', 'H+', 'e-']
    origtime = iono_orig.Time_Vector[0, 0]

    for imult in nomult:
        curiono = iono_orig.copy()
        curiono.Param_List[:, :, 1, 0] *= imult
        ratio = curiono.Param_List[:, :, -1, 0] / curiono.Param_List[:, :, :-1,
                                                                     0].sum(-1)
        ratio_ar = sp.repeat(ratio[:, :, sp.newaxis], nion, axis=-1)
        curiono.Param_List[:, :, :-1, 0] *= ratio_ar
        figout = plotiono(curiono)[0]
        for ibaud in baud_len:
            newpath = basepath.joinpath('NOMult{0:02d}baud{1:02d}'.format(
                int(imult), int(ibaud)))
            newpath.joinpath('Origparams').mkdir(exist_ok=True, parents=True)
            figname = newpath.joinpath('InputParams.png')
            figout.savefig(str(figname))
            dataname = newpath.joinpath('Origparams',
                                        '{0:d}Params.h5'.format(origtime))
            curiono.saveh5(str(dataname))
            simparams['Pulselength'] = ibaud * ts
            sfile = str(newpath / 'startfile.h5')
            iono_orig.saveh5(sfile)
            simparams['startfile'] = sfile
            makeconfigfile(str(newpath / 'chem_test.yml'), [23465.],
                           'millstone', simparams)
def configsetup(testpath):
    """This function will make a pickle file used to configure the simulation.
    Inputs
    testpath - A string for the path that this file will be saved."""
    # list of beams that will give a line of points between 70 and 80 deg el with az of 20 deg
    beamlist = [16626, 16987, 17348, 17709, 18070, 18431, 18792, 19153]#, 19514] # list of beams in
    radarname = 'millstone'# name of radar for parameters can either be pfisr or risr



#    pulse = sp.ones(14)# pulse
    rng_lims = [150,500]# limits of the range gates
    IPP = 1e-2#8.7e-3 #interpulse period in seconds
    NNs = 28 # number of noise samples per pulse
    NNp = 100 # number of noise pulses
    b_rate = 100
    intrate = 2.
    Tint=intrate*b_rate*IPP # integration time in seconds
    time_lim = len(beamlist)/intrate*4.0*Tint # simulation length in seconds
    fitter_int = Tint*len(beamlist)/intrate # time interval between fitted params
    simparams =   {'IPP':IPP, #interpulse period
                   'TimeLim':time_lim, # length of simulation
                   'RangeLims':rng_lims, # range swath limit
#                   'Pulse':pulse, # pulse shape
                   'Pulselength':280e-6,
                   'FitType' :'acf',
                   't_s': 20e-6,
                   'Pulsetype':'long', # type of pulse can be long or barker,
                   'Tint':Tint, #Integration time for each fitting
                   'Fitinter':fitter_int, # time interval between fitted params
                   'NNs': NNs,# number of noise samples per pulse
                   'NNp':NNp, # number of noise pulses
                   'dtype':sp.complex128, #type of numbers used for simulation
                   'ambupsamp':1, # up sampling factor for ambiguity function
                   'species':['O+','e-'], # type of ion species used in simulation
                   'numpoints':128, # number of points for each spectrum
                   'startfile': str(testpath/'startdata.h5'),# file used for starting points
                   'beamrate':b_rate,# the number of pulses each beam will output until it moves
                   'outangles':[sp.arange(i,i+intrate) for i in sp.arange(0,len(beamlist),intrate)]}
#                   'SUMRULE': sp.array([[-2,-3,-3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-9]
#                       ,[1,1,2,2,3,3,4,4,5,5,6,6,7,7]])}

    fname = testpath/'DishExample'

    makeconfigfile(fname.with_suffix('.ini'),beamlist,radarname,simparams)
Exemple #4
0
def makeconfigfilebarker(testpath):
    testpath = Path(testpath).expanduser()

    beamlist = [
        64094, 64091, 64088, 64085, 64082, 64238, 64286, 64070, 64061, 64058,
        64055, 64052, 64049, 64046, 64043, 64067, 64040, 64037, 64034
    ]
    radarname = 'pfisr'

    Tint = 4.0 * 60.0
    time_lim = 3.0 * Tint
    pulse = GenBarker(7)
    rng_lims = [75.0, 250.0]
    IPP = .0087
    NNs = 28
    NNp = 100
    t_s = 2e-5
    Pulselength = len(pulse) * t_s
    simparams = {
        'IPP': IPP,
        'TimeLim': time_lim,
        'RangeLims': rng_lims,
        'Pulse': pulse,
        'Pulsetype': 'barker',
        'Pulselength': Pulselength,
        't_s': t_s,
        'Tint': Tint,
        'Fitinter': Tint,
        'NNs': NNs,
        'NNp': NNp,
        'dtype': sp.complex128,
        'ambupsamp': 30,
        'species': ['O+', 'e-'],
        'numpoints': 128,
        'FitType': 'acf',
        'startfile': str(testpath.joinpath('startdata.h5'))
    }

    fn = testpath / 'barkertest.yml'

    makeconfigfile(fn, beamlist, radarname, simparams)
Exemple #5
0
def makedatasets(maindir='~/DATA/Ion_Comp_Exp/', nomult=sp.arange(1, 5, dtype=float), baud_len=[7, 14, 21]):
    """
        This will make all of the data sets.
    """
    basepath = Path(maindir).expanduser()
    basepath.mkdir(exist_ok=True)
    iono_orig = pyglowinput()
    nion = len(iono_orig.Species)-1
    npulses = 1000
    (sensdict, simparams) = getdefualtparams()
    tint = simparams['IPP']*npulses
    ratio1 = tint/simparams['Tint']
    simparams['RangeLims'] = [80., 750.]
    simparams['Tint'] = ratio1 * simparams['Tint']
    simparams['Fitinter'] = ratio1 * simparams['Fitinter']
    simparams['TimeLim'] = 12*tint
    ts = simparams['t_s']
    simparams['beamrate'] = 1
    simparams['outangles'] = [[0]]
    simparams['species'] = ['O+', 'NO+', 'O2+', 'H+', 'e-']
    origtime = iono_orig.Time_Vector[0,0]

    for imult in nomult:
        curiono = iono_orig.copy()
        curiono.Param_List[:, :, 1, 0] *= imult
        ratio = curiono.Param_List[:, :, -1, 0]/curiono.Param_List[:, :, :-1, 0].sum(-1)
        ratio_ar = sp.repeat(ratio[:, :, sp.newaxis], nion, axis=-1)
        curiono.Param_List[:, :, :-1, 0] *= ratio_ar
        figout = plotiono(curiono)[0]
        for ibaud in baud_len:
            newpath = basepath.joinpath('NOMult{0:02d}baud{1:02d}'.format(int(imult), int(ibaud)))
            newpath.joinpath('Origparams').mkdir(exist_ok=True, parents=True)
            figname = newpath.joinpath('InputParams.png')
            figout.savefig(str(figname))
            dataname = newpath.joinpath('Origparams', '{0:d}Params.h5'.format(origtime))
            curiono.saveh5(str(dataname))
            simparams['Pulselength'] = ibaud*ts
            sfile = str(newpath/'startfile.h5')
            iono_orig.saveh5(sfile)
            simparams['startfile'] = sfile
            makeconfigfile(str(newpath/'chem_test.yml'), [23465.], 'millstone', simparams)
Exemple #6
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)
Exemple #7
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)
Exemple #9
0
def configsetup(testpath):
    """This function will make a pickle file used to configure the simulation.
    Inputs
    testpath - A string for the path that this file will be saved."""
    testpath=Path(testpath)
    beamlist = [64094,64091,64088,64085,64082,64238,64286,64070,64061,64058,64055,64052,
                64049,64046,64043,64067,64040,64037,64034] # list of beams in
    radarname = 'pfisr'# name of radar for parameters can either be pfisr or risr

    Tint=60.0 # integration time in seconds
    time_lim = 4.0*Tint # simulation length in seconds
    fitter_int = 60.0 # time interval between fitted params
#    pulse = sp.ones(14)# pulse
    rng_lims = [150,500]# limits of the range gates
    IPP = .0087 #interpulse period in seconds
    NNs = 28 # number of noise samples per pulse
    NNp = 100 # number of noise pulses
    simparams =   {'IPP':IPP, #interpulse period
                   'TimeLim':time_lim, # length of simulation
                   'RangeLims':rng_lims, # range swath limit
#                   'Pulse':pulse, # pulse shape
                   'Pulselength':280e-6,
                   'FitType' :'acf',
                   't_s': 20e-6,
                   'Pulsetype':'long', # type of pulse can be long or barker,
                   'Tint':Tint, #Integration time for each fitting
                   'Fitinter':fitter_int, # time interval between fitted params
                   'NNs': NNs,# number of noise samples per pulse
                   'NNp':NNp, # number of noise pulses
                   'dtype':sp.complex128, #type of numbers used for simulation
                   'ambupsamp':1, # up sampling factor for ambiguity function
                   'species':['O+','e-'], # type of ion species used in simulation
                   'numpoints':128, # number of points for each spectrum
                   'startfile': str(testpath/'startdata.h5')} # file used for starting points
#                   'SUMRULE': sp.array([[-2,-3,-3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-9]
#                       ,[1,1,2,2,3,3,4,4,5,5,6,6,7,7]])}

    fname = testpath/'PFISRExample'

    makeconfigfile(str(fname.with_suffix('.ini')),beamlist,radarname,simparams)
def makefitdirectory(sridir,srifile,fitdir,simdir):

    srifiles = [glob.glob(os.path.join(sridir,'*.dt{0}.h5'.format(i))) for i in range(3)]

    if [] in srifiles:
        srifiles.remove([])
     
    srifilesnew = [[None]*len(srifiles)]*len(srifiles[0])
    for ieln in range(len(srifiles[0])):
        srifilesnew[ieln] =[i[ieln] for i in srifiles ]

         
    
    # make iono conatainers out of the SRI data    
    (ionolag,ionosigs,simparams,sensdict) = SRIACF2iono(srifilesnew)
    
    paramsiono,startiono = SRIparams2iono(srifile)
    # Add diretories that are needed
    mkdirlist = [fitdir,simdir]

    mkdirlist.append( os.path.join(fitdir,'ACF'))
    mkdirlist.append(os.path.join(fitdir,'Origparams'))
    mkdirlist.append(os.path.join(simdir,'Origparams'))
    for idir in mkdirlist:    
        if not os.path.isdir(idir):
            os.mkdir(idir)
    
    # Save ACFs
    ionolag.saveh5(os.path.join(mkdirlist[-3],'00lags.h5'))
    ionosigs.saveh5(os.path.join(mkdirlist[-3],'00sigs.h5'))
    # Save fitted data
    paramsiono.saveh5(os.path.join(mkdirlist[-2],'0 srifits.h5'))
    paramsiono.saveh5(os.path.join(mkdirlist[-1],'0 srifits.h5'))
    # Save start data
    startiono.saveh5(os.path.join(mkdirlist[0],'startdata.h5'))
    startiono.saveh5(os.path.join(mkdirlist[1],'startdata.h5'))
    # Save ini files
    makeconfigfile(os.path.join(mkdirlist[0],'sridata.ini'),simparams['Beamlist'],sensdict['Name'],simparams)
    makeconfigfile(os.path.join(mkdirlist[1],'sridata.ini'),simparams['Beamlist'],sensdict['Name'],simparams)
Exemple #11
0
def makeconfigfilebarker(testpath):
    testpath = Path(testpath).expanduser()

    beamlist = [64094,64091,64088,64085,64082,64238,64286,64070,64061,64058,64055,64052,
                64049,64046,64043,64067,64040,64037,64034]
    radarname = 'pfisr'

    Tint = 4.0*60.0
    time_lim = 3.0*Tint
    pulse = GenBarker(7)
    rng_lims = [75.0,250.0]
    IPP = .0087
    NNs = 28
    NNp = 100
    t_s=2e-5
    Pulselength=len(pulse)*t_s
    simparams =   {'IPP':IPP,
                   'TimeLim':time_lim,
                   'RangeLims':rng_lims,
                   'Pulse':pulse,
                   'Pulsetype':'barker',
                   'Pulselength':Pulselength,
                   't_s':t_s,
                   'Tint':Tint,
                   'Fitinter':Tint,
                   'NNs': NNs,
                   'NNp':NNp,
                   'dtype':sp.complex128,
                   'ambupsamp':30,
                   'species':['O+','e-'],
                   'numpoints':128,
                   'FitType':'acf',
                   'startfile':str(testpath.joinpath('startdata.h5'))}

    fn = testpath/'barkertest.yml'

    makeconfigfile(fn,beamlist,radarname,simparams)
Exemple #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)
Exemple #13
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)
Exemple #14
0
def configsetup(testpath):
    """This function will make a pickle file used to configure the simulation.
    Inputs
    testpath - A string for the path that this file will be saved."""
    testpath = Path(testpath)
    beamlist = [
        64094,
        64091,
        64088,
        64085,
        64082,
        64238,
        64286,
        64070,
        64061,
        64058,
        64055,
        64052,
        64049,
        64046,
        64043,
        64067,
        64040,
        64037,
        64034,
    ]  # list of beams in
    radarname = "pfisr"  # name of radar for parameters can either be pfisr or risr

    Tint = 60.0  # integration time in seconds
    time_lim = 4.0 * Tint  # simulation length in seconds
    fitter_int = 60.0  # time interval between fitted params
    #    pulse = sp.ones(14)# pulse
    rng_lims = [150, 500]  # limits of the range gates
    IPP = 0.0087  # interpulse period in seconds
    NNs = 28  # number of noise samples per pulse
    NNp = 100  # number of noise pulses
    simparams = {
        "IPP": IPP,  # interpulse period
        "TimeLim": time_lim,  # length of simulation
        "RangeLims": rng_lims,  # range swath limit
        #                   'Pulse':pulse, # pulse shape
        "Pulselength": 280e-6,
        "FitType": "acf",
        "t_s": 20e-6,
        "Pulsetype": "long",  # type of pulse can be long or barker,
        "Tint": Tint,  # Integration time for each fitting
        "Fitinter": fitter_int,  # time interval between fitted params
        "NNs": NNs,  # number of noise samples per pulse
        "NNp": NNp,  # number of noise pulses
        "dtype": sp.complex128,  # type of numbers used for simulation
        "ambupsamp": 1,  # up sampling factor for ambiguity function
        "species": ["O+", "e-"],  # type of ion species used in simulation
        "numpoints": 128,  # number of points for each spectrum
        "startfile": str(testpath / "startdata.h5"),
    }  # file used for starting points
    #                   'SUMRULE': sp.array([[-2,-3,-3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-9]
    #                       ,[1,1,2,2,3,3,4,4,5,5,6,6,7,7]])}

    fname = testpath / "PFISRExample"

    makeconfigfile(str(fname.with_suffix(".ini")), beamlist, radarname, simparams)
Exemple #15
0
def configsetup(testpath):
    """This function will make a pickle file used to configure the simulation.
    Inputs
    testpath - A string for the path that this file will be saved."""
    # list of beams that will give a line of points between 70 and 80 deg el with az of 20 deg
    beamlist = [16626, 16987, 17348, 17709, 18070, 18431, 18792,
                19153]  #, 19514] # list of beams in
    radarname = 'millstone'  # name of radar for parameters can either be pfisr or risr

    #    pulse = sp.ones(14)# pulse
    rng_lims = [150, 500]  # limits of the range gates
    IPP = 1e-2  #8.7e-3 #interpulse period in seconds
    NNs = 28  # number of noise samples per pulse
    NNp = 100  # number of noise pulses
    b_rate = 100
    intrate = 2.
    Tint = intrate * b_rate * IPP  # integration time in seconds
    time_lim = len(
        beamlist) / intrate * 4.0 * Tint  # simulation length in seconds
    fitter_int = Tint * len(
        beamlist) / intrate  # time interval between fitted params
    simparams = {
        'IPP':
        IPP,  #interpulse period
        'TimeLim':
        time_lim,  # length of simulation
        'RangeLims':
        rng_lims,  # range swath limit
        #                   'Pulse':pulse, # pulse shape
        'Pulselength':
        280e-6,
        'FitType':
        'acf',
        't_s':
        20e-6,
        'Pulsetype':
        'long',  # type of pulse can be long or barker,
        'Tint':
        Tint,  #Integration time for each fitting
        'Fitinter':
        fitter_int,  # time interval between fitted params
        'NNs':
        NNs,  # number of noise samples per pulse
        'NNp':
        NNp,  # number of noise pulses
        'dtype':
        sp.complex128,  #type of numbers used for simulation
        'ambupsamp':
        1,  # up sampling factor for ambiguity function
        'species': ['O+', 'e-'],  # type of ion species used in simulation
        'numpoints':
        128,  # number of points for each spectrum
        'startfile':
        str(testpath / 'startdata.h5'),  # file used for starting points
        'beamrate':
        b_rate,  # the number of pulses each beam will output until it moves
        'outangles': [
            sp.arange(i, i + intrate)
            for i in sp.arange(0, len(beamlist), intrate)
        ]
    }
    #                   'SUMRULE': sp.array([[-2,-3,-3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-9]
    #                       ,[1,1,2,2,3,3,4,4,5,5,6,6,7,7]])}

    fname = testpath / 'DishExample'

    makeconfigfile(fname.with_suffix('.ini'), beamlist, radarname, simparams)
def SRIRAW2iono(flist,outdir,desrange=[100.,650.],inttime=5*60.,timelim=1200.):
    """ 
        This will take a list of files and save out radar data
        Inputs
            flist - A list of files that will read and turned into the format 
                for the simulator.
            outdir - The directory that that will hold the data in the format
                from radardatasim. A directory will be created called RadarData
                and the data will be saved there.
            inttime - The number of seconds for the integration time.
    """

    # make directory structure
    if not os.path.isdir(outdir):
        os.mkdir(outdir)
    radardatadir = os.path.join(outdir,'Radardata')

    if not os.path.isdir(radardatadir):
        os.mkdir(radardatadir)
    pulsetimes=[]
    outfilelist = []
    pulsenumbers = []
    beam_list_all = []
    noisetimes=[]
    for ifile,filename in enumerate(flist):
        outdict={}
        fullfile = h5file(filename)
        fullfiledict = fullfile.readWholeh5file()

        if fullfiledict['/Site']['Name'] =='Resolute North':
            radarname='risr'
            Pcal=fullfiledict['/Rx']['Bandwidth']*fullfiledict['/Rx']['CalTemp']*v_Boltz
        else:
            radarname='pfisr'
            Pcal=fullfiledict['/Rx']['Bandwidth']*fullfiledict['/Rx']['CalTemp']*v_Boltz

        bco = fullfiledict['/S/Data']['Beamcodes']
        beamlist = bco[0]
        if ifile==0:
            bstart = sp.zeros(len(beamlist),dtype=sp.int32)
        time= fullfiledict['/Time']['UnixTime']
#
        fullfile = h5file(filename)
        fullfiledict = fullfile.readWholeh5file()
        print('Main file being operated on: '+os.path.split(filename)[-1])
        
        acflags = fullfiledict['/S/Data/Acf']['Lags'].flatten()
        nlags = len(acflags)
        lagdown = sp.arange(-sp.floor(float(nlags-1)/2.),0).astype(int)
        lagup = sp.arange(1,sp.ceil(float(nlags+1)/2.)).astype(int)
        # Get the raw samples
        all_data = fullfiledict['/Raw11/Raw/Samples']['Data']
        rawrange = fullfiledict['/Raw11/Raw/Samples']['Range'][0]*1e-3
        rngkeep= sp.where(sp.logical_and(rawrange>=desrange[0],rawrange<desrange[1]))[0]
        
        rngkeep = sp.hstack((lagdown+rngkeep[0],rngkeep,lagup+rngkeep[-1]))
        rawrange=rawrange[rngkeep]
        pulse_times = fullfiledict['/Raw11/Raw/RadacHeader']['RadacTime']
        rawsamps = all_data[:,:,:,0]+1j*all_data[:,:,:,1]
        rawsamps = rawsamps[:,:,rngkeep]
        (nrecs,  np_rec,nrng)=rawsamps.shape
        
        all_beams_mat = fullfiledict['/Raw11/Raw/RadacHeader']['BeamCode']
#        
        pwidth = float(fullfiledict['/S/Data']['Pulsewidth'])
#        
#        # Pull in call and noise material because these will needed for fitting
        beamcodes_cal = fullfiledict['/S/Cal']['Beamcodes']
        cal_pint = fullfiledict['/S/Cal']['PulsesIntegrated']
        caldata = fullfiledict['/S/Cal/Power']['Data']
        noise_pint = fullfiledict['/S/Noise']['PulsesIntegrated']
        noise_pwer = fullfiledict['/S/Noise/Power']['Data']
        noise_data =fullfiledict['/S/Noise/Acf']['Data']
        noise_acf = noise_data[:,:,:,:,0]+1j*noise_data[:,:,:,:,1]
        noise_acf2 = sp.transpose(noise_acf,(0,1,3,2))
        (nbeams,nnrng,nlags)=noise_acf2.shape[1:]
#        
        # use median to avoid large jumps. The difference between the mean and median estimator
        # goes to zero after enough pulses have been originally integrated. From what Im seeing you're
        # close to 64 pulses integrated you this will be off by only a 1/2 % of the true value.
        n_pow_e = sp.nanmedian(noise_pwer,axis=-1)/noise_pint
        
        c_pow_e = sp.nanmedian(caldata,axis=-1)/cal_pint
        
        # Need to adjust for cal and noise
        powmult = Pcal/(c_pow_e-n_pow_e)
        noise_mult = sp.tile(powmult[:,:,sp.newaxis,sp.newaxis],noise_acf2.shape[2:])
        npint_mat = sp.tile(noise_pint[:,:,sp.newaxis,sp.newaxis],noise_acf2.shape[2:])
        noise_acf_out = noise_acf2*noise_mult/npint_mat
        datamult = sp.zeros_like(rawsamps)
        
        for irec,ibeamlist in enumerate(beamcodes_cal):
            for ibpos,ibeam in enumerate(ibeamlist):
                b_idx = sp.where(ibeam==all_beams_mat[irec])[0]
                datamult[irec,b_idx]=sp.sqrt(powmult[irec,ibpos])

        outraw= rawsamps*datamult
        timep =  pulse_times.reshape(nrecs*np_rec)
        beamn = all_beams_mat.reshape(nrecs*np_rec)
        beamnrs = sp.zeros_like(beamn)
        pulsen = sp.ones(beamn.shape,dtype=sp.int32)
        for ibn, ibeam in enumerate(beamlist):
            curlocs = sp.where(beamn==ibeam)[0]
            beamnrs[curlocs] = ibn
            curtime = timep[curlocs]
            cursort = sp.argsort(curtime)
            curlocs_sort = curlocs[cursort]
            pulsen[curlocs_sort]= sp.arange(len(curlocs)) +bstart[ibn]
            bstart[ibn]=bstart[ibn]+len(curlocs)
            
        outdict['RawData']=outraw.reshape(nrecs*np_rec,nrng)
        outdict['RawDatanonoise'] = outdict['RawData']
        outdict['AddedNoise'] = (1./sp.sqrt(2.))*(sp.randn(*outdict['RawData'].shape)+1j*sp.randn(*outdict['RawData'].shape))
        outdict['NoiseDataACF'] = noise_acf_out
        outdict['BeamsNoise'] = bco[0]
        outdict['Beams']= beamnrs
        outdict['Time'] =timep
        outdict['NoiseTime'] = time
        outdict['Pulses']= pulsen
#
        fname = '{0:d} RawData.h5'.format(ifile)
        newfn = os.path.join(radardatadir,fname)
        outfilelist.append(newfn)
        pulsetimes.append(timep)
        pulsenumbers.append(pulsen)
        noisetimes.append(time)
        beam_list_all.append(beamnrs)
        dict2h5(newfn,outdict)
#    # save the information file
    infodict = {'Files':outfilelist,'Time':pulsetimes,'Beams':beam_list_all,'Pulses':pulsenumbers,'NoiseTime':noisetimes,'Range':rawrange}
    dict2h5(os.path.join(radardatadir,'INFO.h5'),infodict)
#
    ts = fullfiledict['/Rx']['SampleTime']
    sumrule = makesumrule('long',fullfiledict['/S/Data']['Pulsewidth'],ts)
#    minrg = -sumrule[0].min()
#    maxrg = len(rng_vec)-sumrule[1].max()
    maxrg = len(rawrange)+sumrule[0].min()
    minrg = sumrule[1].max()
    rng_lims = [rawrange[minrg],rawrange[maxrg]]# limits of the range gates
    IPP = .0087 #interpulse period in seconds
#   
    simparams =   {'IPP':IPP, #interpulse period
                   'TimeLim':timelim, # length of simulation
                   'RangeLims':rng_lims, # range swath limit
#                   'Pulse':pulse, # pulse shape
                   'Pulselength':pwidth,
                   'FitType' :'acf',
                   't_s': ts,
                   'Pulsetype':'long', # type of pulse can be long or barker,
                   'Tint':inttime, #Integration time for each fitting
                   'Fitinter':inttime, # time interval between fitted params
                   'NNs': nnrng+nlags-1,# number of noise samples per pulse
                   'NNp':100, # number of noise pulses
                   'dtype':sp.complex128, #type of numbers used for simulation
                   'ambupsamp':1, # up sampling factor for ambiguity function
                   'species':['O+','e-'], # type of ion species used in simulation
                   'numpoints':128,
                   'startfile':'startfile.h5'
                   } # number of points for each spectrum
    makeconfigfile(os.path.join(outdir,'sriconfig1.ini'),beamlist,radarname,simparams)