def makeinputh5(Iono,basedir): basedir = Path(basedir).expanduser() Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn]=sp.mean(Param_List[arr],axis=0) outvel[izn]=sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) ofn = basedir/'startdata.h5' print('writing {}'.format(ofn)) Ionoout.saveh5(str(ofn))
def makeinputh5(Iono,basedir): """This will make a h5 file for the IonoContainer that can be used as starting points for the fitter. The ionocontainer taken will be average over the x and y dimensions of space to make an average value of the parameters for each altitude. Inputs Iono - An instance of the Ionocontainer class that will be averaged over so it can be used for fitter starting points. basdir - A string that holds the directory that the file will be saved to. """ # Get the parameters from the original data Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) # Do the averaging across space for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn] = sp.mean(Param_List[arr],axis=0) outvel[izn] = sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) Ionoout.saveh5(basedir/'startdata.h5')
def makedata(testpath): """ This will make the input data for the test case. The data will have the default set of parameters Ne=Ne=1e11 and Te=Ti=2000. Inputs testpath - Directory that will hold the data. """ finalpath = testpath.joinpath('Origparams') if not finalpath.exists(): finalpath.mkdir() data=SIMVALUES z = sp.linspace(50.,1e3,50) nz = len(z) params = sp.tile(data[sp.newaxis,sp.newaxis,:,:],(nz,1,1,1)) coords = sp.column_stack((sp.ones(nz),sp.ones(nz),z)) species=['O+','e-'] times = sp.array([[0,1e3]]) vel = sp.zeros((nz,1,3)) Icont1 = IonoContainer(coordlist=coords,paramlist=params,times = times,sensor_loc = sp.zeros(3),ver =0,coordvecs = ['x','y','z'],paramnames=None,species=species,velocity=vel) finalfile = finalpath.joinpath('0 stats.h5') Icont1.saveh5(str(finalfile)) # set start temp to 1000 K. Icont1.Param_List[:,:,:,1]=1e3 Icont1.saveh5(str(testpath.joinpath('startfile.h5')))
def makedata(testpath): """ This will make the input data for the test case. The data will have the default set of parameters Ne=Ne=1e11 and Te=Ti=2000. Inputs testpath - Directory that will hold the data. """ finalpath = testpath.joinpath('Origparams') if not finalpath.exists(): finalpath.mkdir() 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)) Icont1 = IonoContainer(coordlist=coords, paramlist=params, times=times, sensor_loc=sp.zeros(3), ver=0, coordvecs=['x', 'y', 'z'], paramnames=None, species=species, velocity=vel) finalfile = finalpath.joinpath('0 stats.h5') Icont1.saveh5(str(finalfile)) # set start temp to 1000 K. Icont1.Param_List[:, :, :, 1] = 1e3 Icont1.saveh5(str(testpath.joinpath('startfile.h5')))
def makeinputh5(Iono,basedir): """This will make a h5 file for the IonoContainer that can be used as starting points for the fitter. The ionocontainer taken will be average over the x and y dimensions of space to make an average value of the parameters for each altitude. Inputs Iono - An instance of the Ionocontainer class that will be averaged over so it can be used for fitter starting points. basdir - A string that holds the directory that the file will be saved to. """ # Get the parameters from the original data Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) # Do the averaging across space for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn]=sp.mean(Param_List[arr],axis=0) outvel[izn]=sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) Ionoout.saveh5(str(basedir/'startdata.h5'))
def makeinputh5(Iono, basedir): basedir = Path(basedir).expanduser() Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist, idx = sp.unique(dataloc[:, 2], return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack( (sp.zeros_like(zlist), sp.zeros_like(zlist), zlist)) outdata = sp.zeros([len(zlist)] + siz) outvel = sp.zeros([len(zlist)] + vsiz) for izn, iz in enumerate(zlist): arr = sp.argwhere(idx == izn) outdata[izn] = sp.mean(Param_List[arr], axis=0) outvel[izn] = sp.mean(velocity[arr], axis=0) Ionoout = IonoContainer(datalocsave, outdata, times, Iono.Sensor_loc, ver=0, paramnames=Iono.Param_Names, species=Iono.Species, velocity=outvel) ofn = basedir / 'startdata.h5' print('writing {}'.format(ofn)) Ionoout.saveh5(str(ofn))
def makestartfile(datapath): outdata = sp.array([[[[1e11,3e3],[1e11,3e3]]]]) datalocs = sp.array([[225.0,15.0,89.0]]) pnames = sp.array([['Ni','Ti'],['Ne','Te']]) species=['O2+','e-'] vel = sp.array([[[0,0,0]]]) Ionoout = IonoContainer(datalocs,outdata,sp.array([0]),ver=1, paramnames=pnames, species=species,velocity=vel) Ionoout.saveh5(os.path.join(datapath,'startdata.h5'))
def makedata(testpath, tint): """ This will make the input data for the test case. The data will have cases where there will be enhancements in Ne, Ti and Te in one location. Each case will have 3 integration periods. The first 3 integration periods will be the default set of parameters Ne=Ne=1e11 and Te=Ti=2000. Inputs testpath - Directory that will hold the data. tint - The integration time in seconds. """ testpath = Path(testpath).expanduser() finalpath = testpath.joinpath('Origparams') if not finalpath.is_dir(): finalpath.mkdir() data = sp.array([[1e11, 1100.], [1e11, 2100.]]) z = (50. + sp.arange(50) * 10.) nz = len(z) params = sp.tile(data[sp.newaxis, sp.newaxis], (nz, 1, 1, 1)) epnt = range(20, 22) p2 = sp.tile(params, (1, 4, 1, 1)) #enhancement in Ne p2[epnt, 1, :, 0] = 5e11 #enhancement in Ti p2[epnt, 2, 0, 1] = 2200. #enhancement in Te p2[epnt, 3, 1, 1] = 4200. coords = sp.column_stack((sp.ones(nz), sp.ones(nz), z)) species = ['O+', 'e-'] times = sp.array([[0, 1e3]]) times2 = sp.column_stack((sp.arange(0, 4), sp.arange(1, 5))) * 3 * tint vel = sp.zeros((nz, 1, 3)) vel2 = sp.zeros((nz, 4, 3)) Icontstart = IonoContainer(coordlist=coords, paramlist=params, times=times, sensor_loc=sp.zeros(3), ver=0, coordvecs=['x', 'y', 'z'], paramnames=None, species=species, velocity=vel) Icont1 = IonoContainer(coordlist=coords, paramlist=p2, times=times2, sensor_loc=sp.zeros(3), ver=0, coordvecs=['x', 'y', 'z'], paramnames=None, species=species, velocity=vel2) finalfile = finalpath.joinpath('0 stats.h5') Icont1.saveh5(str(finalfile)) Icontstart.saveh5(str(testpath.joinpath('startfile.h5')))
def makedata(testpath,tint): """ This will make the input data for the test case. The data will have cases where there will be enhancements in Ne, Ti and Te in one location. Each case will have 3 integration periods. The first 3 integration periods will be the default set of parameters Ne=Ne=1e11 and Te=Ti=2000. Inputs testpath - Directory that will hold the data. tint - The integration time in seconds. """ testpath = Path(testpath).expanduser() finalpath = testpath.joinpath('Origparams') if not finalpath.is_dir(): finalpath.mkdir() data = sp.array([[1e11,1100.], [1e11,2100.]]) z = (50.+sp.arange(50)*10.) nz = len(z) params = sp.tile(data[sp.newaxis, sp.newaxis],(nz, 1, 1, 1)) epnt = range(20,22) p2 = sp.tile(params, (1, 4, 1, 1)) #enhancement in Ne p2[epnt, 1, :, 0] = 5e11 #enhancement in Ti p2[epnt,2,0,1] = 2200. #enhancement in Te p2[epnt,3,1,1] = 4200. coords = sp.column_stack((sp.zeros(nz), sp.zeros(nz), z)) species=['O+', 'e-'] times = sp.array([[0, 1e3]]) times2 = sp.column_stack((sp.arange(0, 4), sp.arange(1, 5)))*3*tint vel = sp.zeros((nz, 1, 3)) vel2 = sp.zeros((nz, 4, 3)) Icontstart = IonoContainer(coordlist=coords, paramlist=params, times=times, sensor_loc=sp.zeros(3), ver=0, coordvecs=['x', 'y', 'z'], paramnames=None, species=species, velocity=vel) Icont1 = IonoContainer(coordlist=coords, paramlist=p2, times=times2, sensor_loc=sp.zeros(3), ver=0, coordvecs=['x', 'y', 'z'], paramnames=None, species=species, velocity=vel2) finalfile = finalpath.joinpath('0 stats.h5') Icont1.saveh5(str(finalfile)) Icontstart.saveh5(str(testpath.joinpath('startfile.h5')))
def makedata(testpath,tint): """ This will make the input data for the test case. The data will have cases where there will be enhancements in Ne, Ti and Te in one location. Each case will have 3 integration periods. The first 3 integration periods will be the default set of parameters Ne=Ne=1e11 and Te=Ti=2000. Inputs testpath - Directory that will hold the data. tint - The integration time in seconds. """ finalpath = os.path.join(testpath,'Origparams') if not os.path.isdir(finalpath): os.mkdir(finalpath) z = sp.linspace(50.,750,150) nz = len(z) Z_0 = 250. H_0=30. N_0=6.5e11 c1 = Chapmanfunc(z,H_0,Z_0,N_0)+5e10 z0=50. T0=600. Te,Ti = TempProfile(z,T0,z0) params = sp.zeros((nz,1,2,2)) params[:,0,0,0] = c1 params[:,0,1,0] = c1 params[:,0,0,1] = Ti params[:,0,1,1] = Te coords = sp.column_stack((sp.zeros(nz),sp.zeros(nz),z)) species=['O+','e-'] times = sp.array([[0,1e3]]) times2 = sp.column_stack((sp.arange(0,1),sp.arange(1,2)))*3*tint vel = sp.zeros((nz,1,3)) vel2 = sp.zeros((nz,4,3)) Icontstart = IonoContainer(coordlist=coords,paramlist=params,times = times,sensor_loc = sp.zeros(3),ver =0,coordvecs = ['x','y','z'],paramnames=None,species=species,velocity=vel) Icont1 = IonoContainer(coordlist=coords,paramlist=params,times = times,sensor_loc = sp.zeros(3),ver =0,coordvecs = ['x','y','z'],paramnames=None,species=species,velocity=vel2) finalfile = os.path.join(finalpath,'0 stats.h5') Icont1.saveh5(finalfile) Icontstart.saveh5(os.path.join(testpath,'startfile.h5'))
def run_sim(args_commd): fitoutput = fitterone.fitdata(specfuncs.ISRSfitfunction, fitterone.simparams['startfile'], fittimes=fitlist, printlines=printlines) (fitteddata, fittederror, funcevals, fittedcov) = fitoutput fittederronly = sp.sqrt(fittederror) paramnames = [] species = fitterone.simparams['species'] # Seperate Ti and put it in as an element of the ionocontainer. Ti = fitteddata[:, :, 1] nTi = fittederronly[:, :, 1] nTiTe = fittedcov[:, :, 0, 1] nTiNe = fittedcov[:, :, 0, 2] nTiVi = fittedcov[:, :, 0, 3] nTeNe = fittedcov[:, :, 1, 2] nTeVi = fittedcov[:, :, 1, 3] nNeVi = fittedcov[:, :, 2, 3] cov_list = [nTiTe[:, :, sp.newaxis], nTiNe[:, :, sp.newaxis], nTiVi[:, :, sp.newaxis], nTeNe[:, :, sp.newaxis], nTeVi[:, :, sp.newaxis], nNeVi[:, :, sp.newaxis]] cov_list_names = ['nTiTe', 'nTiNe', 'nTiVi', 'nTeNe', 'nTeVi','nNeVi'] paramlist = sp.concatenate([fitteddata, Ti[:, :, sp.newaxis], fittederronly, nTi[:, :, sp.newaxis], funcevals[:, :, sp.newaxis]] + cov_list, axis=2) ionoout = IonoContainer(Ionoin.Cart_Coords, paramlist.real, timevec, ver=newver, coordvecs=Ionoin.Coord_Vecs, paramnames=paranamsf, species=species) ionoout.saveh5(str(outfile))
def readmattsdata(filename,datadir,outdir,keepspec=[0,1,2,6],angle=20.5): d2r=sp.pi/180. angr=d2r*angle lsp=7 # Read in Data inst = sio.loadmat(os.path.join(datadir,filename)) xg=inst['xg'][0,0] x1v = xg['xp']# added to avoid gratting lobes. x3v = xg['zp'] [x1mat,x3mat] = sp.meshgrid(x1v,x3v); E = x1mat*sp.sin(angr)#x N = x1mat*sp.cos(angr)#y U = x3mat lxs=x3mat.size Time_Vector = sp.column_stack([inst['t'],inst['t']+15]) ns =inst['ns'] print('Loaded densities...'); ns= sp.reshape(ns,[lxs,lsp]) Ts =inst['Ts'] print('Loaded temperatures...') Ts=sp.reshape(Ts,[lxs,lsp]) vs = inst['vsx1'] print('Loaded parallel velocities...\n'); # derive velocity from ExB Ez,Ex=sp.gradient(-1*inst['Phi']) dx=sp.diff(xg['x'].flatten())[0] dEdx=Ex/dx vx1=-1*dEdx/xg['Bmag'] # from looking at the data it seems that the velocity is off by a factor of # 10. vx1=vx1.flatten()/10. vs=sp.reshape(vs,[lxs,lsp]) vs=sp.sum(ns[:,:(lsp-1)]*vs[:,:(lsp-1)],1)/ns[:,lsp-1] v_e= vx1*sp.sin(angr) v_n = vx1*sp.cos(angr) v_u = vs #change units of velocity to km/s Velocity = sp.reshape(sp.column_stack([v_e,v_n,v_u]),[lxs,1,3]) # reduce the number of spcecies # if islogical(keepspec) # keepspec(end)=true; # keepspecnum = sum(keepspec); # elseif ~any(keepspec==numspec) # keepspec = [keepspec(:),numspec]; # keepspecnum = length(keepspec); # else # keepspecnum = length(keepspec); # end nsout = sp.reshape(ns[:,keepspec],[lxs,1,len(keepspec)]) Tsout = sp.reshape(Ts[:,keepspec],[lxs,1,len(keepspec)]) # Put everything in to ionocontainer structure Cart_Coords = sp.column_stack([E.flatten(),N.flatten(),U.flatten()])*1e-3 Param_List = sp.concatenate((sp.expand_dims(nsout,nsout.ndim),sp.expand_dims(Tsout,Tsout.ndim)),-1); Species = sp.array(['O+','NO+','N2+','O2+','N+', 'H+','e-']) Species = Species[keepspec] fpart=os.path.splitext(filename)[0] fout=os.path.join(outdir,fpart+'.h5') ionoout=IonoContainer(Cart_Coords,Param_List,Time_Vector,ver=0,species=Species,velocity=Velocity) ionoout.saveh5(fout)
def fitdata(basedir,configfile,optinputs): """ This function will run the fitter on the estimated ACFs saved in h5 files. Inputs: basedir: A string for the directory that will hold all of the data for the simulation. configfile: The configuration file for the simulation. optinputs:A string that helps determine the what type of acfs will be fitted. """ # determine the input folders which can be ACFs from the full simulation dirdict = {'fitting':('ACF', 'Fitted'), 'fittingmat':('ACFMat', 'FittedMat'), 'fittinginv':('ACFInv', 'FittedInv'), 'fittingmatinv':('ACFMatInv', 'FittedMatInv')} dirio = dirdict[optinputs[0]] inputdir = basedir/dirio[0] outputdir = basedir/dirio[1] fitlist = optinputs[1] if len(optinputs) > 2: exstr = optinputs[2] printlines = optinputs[3] else: exstr = '' dirlist = [str(i) for i in inputdir.glob('*lags{0}.h5'.format(exstr))] dirlistsig = [str(i) for i in inputdir.glob('*sigs{0}.h5'.format(exstr))] Ionoin = IonoContainer.readh5(dirlist[0]) if len(dirlistsig) == 0: Ionoinsig = None else: Ionoinsig = IonoContainer.readh5(dirlistsig[0]) fitterone = Fitterionoconainer(Ionoin, Ionoinsig, configfile) fitoutput = fitterone.fitdata(specfuncs.ISRSfitfunction, fitterone.simparams['startfile'], fittimes=fitlist, printlines=printlines) #ipdb.set_trace() (fitteddata, fittederror, funcevals, fittedcov) = fitoutput if fitterone.simparams['Pulsetype'].lower() == 'barker': paramlist = fitteddata species = fitterone.simparams['species'] paramnames = ['Ne'] if not fittederror is None: fittederronly = sp.sqrt(fittederror) paramlist = sp.concatenate([fitteddata, fittederronly], axis=2) paramnamese = ['n'+ip for ip in paramnames] paranamsf = sp.array(paramnames+paramnamese) else: paranamsf = sp.array(paramnames) else: fittederronly = sp.sqrt(fittederror) paramnames = [] species = fitterone.simparams['species'] # Seperate Ti and put it in as an element of the ionocontainer. Ti = fitteddata[:, :, 1] nTi = fittederronly[:, :, 1] nTiTe = fittedcov[:, :, 0, 1] nTiNe = fittedcov[:, :, 0, 2] nTiVi = fittedcov[:, :, 0, 3] nTeNe = fittedcov[:, :, 1, 2] nTeVi = fittedcov[:, :, 1, 3] nNeVi = fittedcov[:, :, 2, 3] cov_list = [nTiTe[:, :, sp.newaxis], nTiNe[:, :, sp.newaxis], nTiVi[:, :, sp.newaxis], nTeNe[:, :, sp.newaxis], nTeVi[:, :, sp.newaxis], nNeVi[:, :, sp.newaxis]] cov_list_names = ['nTiTe', 'nTiNe', 'nTiVi', 'nTeNe', 'nTeVi','nNeVi'] paramlist = sp.concatenate([fitteddata, Ti[:, :, sp.newaxis], fittederronly, nTi[:, :, sp.newaxis], funcevals[:, :, sp.newaxis]] + cov_list, axis=2) for isp in species[:-1]: paramnames.append('Ni_'+isp) paramnames.append('Ti_'+isp) paramnames = paramnames+['Ne', 'Te', 'Vi', 'Nepow', 'Ti'] paramnamese = ['n'+ip for ip in paramnames] paranamsf = sp.array(paramnames+paramnamese+['FuncEvals']+cov_list_names) if fitlist is None: timevec = Ionoin.Time_Vector else: if len(fitlist) == 0: timevec = Ionoin.Time_Vector else: timevec = Ionoin.Time_Vector[fitlist] # This requires if set(Ionoin.Coord_Vecs) == {'x', 'y', 'z'}: newver = 0 ionoout = IonoContainer(Ionoin.Cart_Coords, paramlist.real, timevec, ver=newver, coordvecs=Ionoin.Coord_Vecs, paramnames=paranamsf, species=species) elif set(Ionoin.Coord_Vecs) == {'r', 'theta', 'phi'}: newver = 1 ionoout = IonoContainer(Ionoin.Sphere_Coords, paramlist.real, timevec, ver=newver, coordvecs=Ionoin.Coord_Vecs, paramnames=paranamsf, species=species) outfile = outputdir.joinpath('fitteddata{0}.h5'.format(exstr)) ionoout.saveh5(str(outfile))