diff --git a/python/cosmotool/__init__.py b/python/cosmotool/__init__.py index 70fc1f1..bf0df59 100644 --- a/python/cosmotool/__init__.py +++ b/python/cosmotool/__init__.py @@ -1 +1,2 @@ from _cosmotool import * +from borg import read_borg_vol diff --git a/python/cosmotool/borg.py b/python/cosmotool/borg.py new file mode 100644 index 0000000..5ac0f7b --- /dev/null +++ b/python/cosmotool/borg.py @@ -0,0 +1,265 @@ +### +### BORG code is from J. Jasche +### +import StringIO +import numpy as np +from numpy import * +import os.path +import array +import glob + +class BorgVolume(object): + + def __init__(self, density, ranges): + self._density = density + self._ranges = ranges + + property density: + def __get__(self): + return self._density + + property ranges: + def __get__(self): + return self._ranges + +def build_filelist(fdir): + #builds list of all borg density fields which may be distributed over several directories + + fname_0=glob.glob(fdir[0]+'initial_density_*') + fname_1=glob.glob(fdir[0]+'final_density_*') + + fdir=fdir[1:] #eliminate first element + + for fd in fdir: + fname_0=fname_0+glob.glob(fd+'initial_density_*') + fname_1=fname_1+glob.glob(fd+'final_density_*') + + + return fname_0, fname_1 + +def read_borg_vol(BORGFILE): + """ Reading routine for BORG data + """ + + openfile=open(BORGFILE,'rb') + + period=0 + N0=0 + N1=0 + N2=0 + + xmin=0 + xmax=0 + + ymin=0 + ymax=0 + + zmin=0 + zmax=0 + nlines=0 + + while True: + line=openfile.readline() + s=line.rstrip('\n') + r=s.rsplit(' ') + + if size(r)==5 : + if r[0] =="define": + if r[1]=="Lattice" : N0=int(r[2]) + N1=int(r[3]) + N2=int(r[4]) + + + if size(r)==11 : + if r[4] =="BoundingBox": xmin=float(r[5]) + xmax=float(r[6]) + ymin=float(r[7]) + ymax=float(r[8]) + zmin=float(r[9]) + zmax=float(r[10].rstrip(',')) + + if r[0]=='@1': break + + ranges=[] + ranges.append(xmin) + ranges.append(xmax) + ranges.append(ymin) + ranges.append(ymax) + ranges.append(zmin) + ranges.append(zmax) + + #now read data + data=np.fromfile(openfile, '>f4') + data=data.reshape(N2,N0,N1) + return BorgVolume(data,ranges) + +def read_spec( fname ): + """ Reading routine for ARES spectrum samples + """ + x,y=np.loadtxt( fname ,usecols=(0,1),unpack=True) + + return x , y + + +def read_bias_nmean( fname ): + """ Reading routine for ARES bias data + """ + x,b0,b1,nmean=np.loadtxt( fname ,usecols=(0,1,2,3),unpack=True) + + return x , b0, b1, nmean + +def read_nmean( fname ): + """ Reading routine for BORG bias data + """ + x,nmean=np.loadtxt( fname ,usecols=(0,1),unpack=True) + + return x, nmean + +def get_grid_values(xx,data, ranges): + """ return values at grid positions + """ + xmin=ranges[0] + xmax=ranges[1] + + ymin=ranges[2] + ymax=ranges[3] + + zmin=ranges[4] + zmax=ranges[5] + + Lx= xmax-xmin + Ly= ymax-ymin + Lz= zmax-zmin + + Nx=shape(data)[0] + Ny=shape(data)[1] + Nz=shape(data)[2] + + dx=Lx/float(Nx) + dy=Ly/float(Ny) + dz=Lz/float(Nz) + + idx=(xx[:,0]-xmin)/dx + idy=(xx[:,1]-ymin)/dz + idz=(xx[:,2]-zmin)/dy + + idx=idx.astype(int) + idy=idy.astype(int) + idz=idz.astype(int) + + idflag=np.where( (idx>-1)*(idx-1)*(idy-1)*(idz