Skip to content

Commit

Permalink
Redesign load
Browse files Browse the repository at this point in the history
  • Loading branch information
henry2004y committed Dec 12, 2023
1 parent c747d5e commit 2011af8
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 64 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ With JuliaCall:
```python
from juliacall import Main as jl
jl.seval("using Batsrus")
file = 'example.out'
data = Batsrus.load(file, dir='test')
file = 'test/example.out'
data = Batsrus.load(file)
```

With PyJulia:

```python
from julia import Batsrus
file = 'example.out'
data = Batsrus.load(file, dir='test')
file = 'test/example.out'
data = Batsrus.load(file)
```

## Benchmark
Expand Down
9 changes: 4 additions & 5 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,16 @@ With JuliaCall:
```python
from juliacall import Main as jl
jl.seval("using Batsrus")
file = 'example.out'
data = Batsrus.load(file, dir='test')
file = 'test/example.out'
data = Batsrus.load(file)
```

With PyJulia:

```python
from julia import Batsrus
dir = 'test'
file = '1d__raw_2_t25.60000_n00000258.out'
data = Batsrus.load(file, dir=dir)
file = 'test/1d__raw_2_t25.60000_n00000258.out'
data = Batsrus.load(file)
```

!!! warning "Python dependency"
Expand Down
5 changes: 2 additions & 3 deletions docs/src/man/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ file = "1d_bin.out";
data = load(file);
data = load(file, verbose=true);
data = load(file, npict=1);
data = load(file, dir=".");
```

- 3D structured spherical coordinates
Expand Down Expand Up @@ -287,8 +286,8 @@ An example of tracing in a 2D cut and plot the field lines over contour:
```julia
using Batsrus, PyPlot

file = "y=0_var_1_t00000000_n00000000.out"
data = load(file, dir="test")
file = "test/y=0_var_1_t00000000_n00000000.out"
data = load(file)

bx = data.w[:,:,5]
bz = data.w[:,:,7]
Expand Down
6 changes: 2 additions & 4 deletions examples/pic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ inset_axes = inset_locator.inset_axes
DN = matplotlib.colors.DivergingNorm

dir = "."
fnameField = "3d_var_region0_0_t00001640_n00020369.out"
#fnameField = "3d_var_region0_0_t00001520_n00004093.out"
file = "3d_var_region0_0_t00001640_n00020369.out"

data = load(fnameField, dir=dir)
data = load(joinpath(dir, file))

me = data.head.eqpar[1]
qe = data.head.eqpar[2]
Expand All @@ -32,7 +31,6 @@ const J₀ = 4.0*vAlfven # Actual normalization unit: q*4.0*vAlfven
const T₀ = 0.2/4 # Pe/n₀

plotrange = [-2.05, -1.75, -0.5, 0.5]
#plotrange = [-2.12, -1.75, -0.65, 0.6]
#plotrange=[-Inf, Inf, -Inf, Inf]
cI = 129 # plane cut index

Expand Down
6 changes: 3 additions & 3 deletions examples/plot_batch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nfiles = length(filenames)
fig, ax = subplots(figsize=(10,7.5))

@info "1 / $nfiles, $(basename(filenames[1]))"
data = load(basename(filenames[1]); dir)
data = load(filenames[1])
varIndex_ = Batsrus.findindex(data, var)
cnorm, cticks = @views Batsrus.set_colorbar(colorscale, vmin, vmax, data.w[:,:,varIndex_])

Expand All @@ -37,9 +37,9 @@ plt.cla()

if nfiles > 1
for i in 2:nfiles
f = filenames[i] |> basename
f = filenames[i]
@info "$i / $nfiles, $f"
local data = load(f; dir)
local data = load(f)

contourf(data, var, levels; ax, plotrange, plotinterval, innermask=true, norm=cnorm)

Expand Down
44 changes: 21 additions & 23 deletions examples/vdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function dist_select(fnameParticle, xC=-1.90, yC=0.0, zC=-0.1, xL=0.005, yL=0.2,
!occursin("region0_1", fnameParticle) && @error "Check filename!"
end

fnameField = "3d_var_region0_0_"*fnameParticle[end-22:end]
filefield = "3d_var_region0_0_"*fnameParticle[end-22:end]

nBox = 9 # number of box regions

Expand All @@ -48,7 +48,7 @@ function dist_select(fnameParticle, xC=-1.90, yC=0.0, zC=-0.1, xL=0.005, yL=0.2,

particle = [Array{Float32}(undef, 3, 0) for _ in 1:nBox]

data = load(fnameParticle; dir)
data = load(joinpath(dir, fnameParticle))

x = @view data.x[:,:,:,1]
y = @view data.x[:,:,:,2]
Expand Down Expand Up @@ -83,7 +83,7 @@ Velocity distribution plot in 9 box regions.
`plottype`: 1: uy-ux; 2: ux-uz; 3:uy-uz; 4:u⟂O-u⟂I; 5:u⟂I-u∥; 5:u⟂O-u∥
"""
function dist_scan(region, particle, ParticleType='i', plottype=1; dir=".",
fnameField::AbstractString, nbin=60, fs=10)
filefield::AbstractString, nbin=60, fs=10)

if ParticleType == 'i'
binRange = [[-3.,3.], [-3.,3.]]
Expand All @@ -99,7 +99,7 @@ function dist_scan(region, particle, ParticleType='i', plottype=1; dir=".",
uy = particle[iB][2,:] ./ cAlfven
uz = particle[iB][3,:] ./ cAlfven
else
dBx, dBy, dBz = GetMeanField(fnameField, region[:,iB]; dir)
dBx, dBy, dBz = GetMeanField(filefield, region[:,iB]; dir)

dPar = [dBx; dBy; dBz] # Parallel direction
dPerpI = cross([0; -1; 0], dPar) # Perpendicular direction in-plane
Expand Down Expand Up @@ -173,14 +173,12 @@ function dist_scan(region, particle, ParticleType='i', plottype=1; dir=".",
end

"""
GetMeanField(fnameField, limits; dir=".")
GetMeanField(filefield, limits; dir=".")
Get the average field direction in region `limits`.
Get the average field direction in a box region specified by `limits`.
"""
function GetMeanField(fnameField, limits; dir=".")

# Get the average field direction in limited region
data = load(fnameField; dir)
function GetMeanField(filefield, limits; dir=".")
data = load(joinpath(dir, filefield))

x = data.x[:,:,:,1]
y = data.x[:,:,:,2]
Expand Down Expand Up @@ -208,12 +206,12 @@ end



function plotExCut(fnameField::String, region, xC, yC, zC, xL, yL, zL;
function plotExCut(filefield::String, region, xC, yC, zC, xL, yL, zL;
dir=".", fs=16, sequence=129)

plotrange = [xC-xL*16, xC+xL*16, zC-zL*5, zC+zL*5]
# Sample region plot over contour
data = load(fnameField, dir=dir)
data = load(joinpath(dir, filefield))

bx_ = findfirst(x->x=="Bx", data.head.wnames)
bz_ = findfirst(x->x=="Bz", data.head.wnames)
Expand Down Expand Up @@ -256,7 +254,7 @@ function dist_plot(pType='e')
fnameE = "cut_particles0_region0_1_t00001640_n00020369.out"
fnameI = "cut_particles1_region0_2_t00001640_n00020369.out"

fnameField = "3d_var_region0_0_"*fnameE[end-22:end]
filefield = "3d_var_region0_0_"*fnameE[end-22:end]

binRangeI = [[-3.,3.], [-3.,3.]]
binRangeE = [[-7.,13.], [-10.,10.]]
Expand All @@ -278,7 +276,7 @@ function dist_plot(pType='e')
region[:,3] = [-1.930, -1.925, -0.08, 0.08, -0.10, -0.06]
region[:,4] = [-1.930, -1.925, -0.08, 0.08, 0.00, 0.04]

data = load(fnameE, dir=dir)
data = load(joinpath(dir, fnameE))

x = @view data.x[:,:,:,1]
y = @view data.x[:,:,:,2]
Expand Down Expand Up @@ -317,7 +315,7 @@ function dist_plot(pType='e')
region[:,3] = [-1.930, -1.920, -0.08, 0.08, -0.10, -0.06]
region[:,4] = [-1.930, -1.920, -0.08, 0.08, 0.00, 0.04]

data = load(fnameI, dir=dir)
data = load(joinpath(dir, fnameI))

x = @view data.x[:,:,:,1]
y = @view data.x[:,:,:,2]
Expand Down Expand Up @@ -368,7 +366,7 @@ function dist_plot(pType='e')
uy = particle[iB][2,:] ./ cAlfven
uz = particle[iB][3,:] ./ cAlfven
else
dBx, dBy, dBz = GetMeanField(fnameField, region[:,iB]; dir=dir)
dBx, dBy, dBz = GetMeanField(filefield, region[:,iB]; dir=dir)

dPar = [dBx; dBy; dBz] # Parallel direction
dPerpI = cross([0; -1; 0], dPar) # Perpendicular direction in-plane
Expand Down Expand Up @@ -477,13 +475,13 @@ function show_box_region()
fnameE = "cut_particles0_region0_1_t00001640_n00020369.out"
fnameI = "cut_particles1_region0_2_t00001640_n00020369.out"

fnameField = "3d_var_region0_0_"*fnameE[end-22:end]
filefield = "3d_var_region0_0_"*fnameE[end-22:end]

fs = 10
plotrange = [-2.0, -1.8, -0.35, 0.35]
sequence = 129 # cut plane index starting from -

data = load(fnameField; dir)
data = load(joinpath(dir, filefield())

X, Z, Bx = cutdata(data, "Bx"; dir="y", sequence, plotrange)
X, Z, Bz = cutdata(data, "Bz"; dir="y", sequence, plotrange)
Expand Down Expand Up @@ -583,9 +581,9 @@ function HF_velocity()
fnameE = "cut_particles0_region0_1_t00001640_n00020369.out"
fnameI = "cut_particles1_region0_2_t00001640_n00020369.out"

fnameField = "3d_var_region0_0_"*fnameE[end-22:end]
filefield = "3d_var_region0_0_"*fnameE[end-22:end]

data = load(fnameField; dir)
data = load(joinpath(dir, filefield))

x = data.x[:,:,:,1]
y = data.x[:,:,:,2]
Expand All @@ -612,7 +610,7 @@ end


dir = "/Users/hyzhou/Documents/Computer/DeepBlue"
fnameField = "3d_var_region0_0_t00001640_n00020369.out"
filefield = "3d_var_region0_0_t00001640_n00020369.out"
PType = 'e'
plottype = 2

Expand All @@ -633,9 +631,9 @@ xL, yL, zL = 0.008, 0.2, 0.03 # box length in x,y,z
fnameParticle, xC, yC, zC, xL, yL, zL,
dir=dir, ParticleType=PType)

@time dist_scan(region, particle, PType, plottype; dir, fnameField)
@time dist_scan(region, particle, PType, plottype; dir, filefield)

@time plotExCut(fnameField, region, xC,yC,zC,xL,yL,zL; dir)
@time plotExCut(filefield, region, xC,yC,zC,xL,yL,zL; dir)


#ax = dist_plot('i')
Expand Down
10 changes: 5 additions & 5 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const TAG = 4 # Fortran record tag size
searchdir(path, key) = filter(x->occursin(key, x), readdir(path))

"""
load(filenameIn; dir=".", npict=1, verbose=false)
load(filename; npict=1, verbose=false)
Read BATSRUS output files. Stores the `npict` snapshot from an ascii or binary data file
into the arrays of coordinates `x` and data `w`. Filename can be provided with wildcards.
Expand All @@ -17,14 +17,14 @@ into the arrays of coordinates `x` and data `w`. Filename can be provided with w
data = load("1d_raw*")
```
"""
function load(filenameIn::AbstractString; dir::String=".", npict::Int=1,
verbose::Bool=false)
function load(filenameIn::AbstractString; npict::Int=1, verbose::Bool=false)
# Check the existence of files
file = searchdir(dir, Regex(replace(filenameIn, s"*" => s".*")))
dir, filename = splitdir(filenameIn)
file = searchdir(dir, Regex(replace(filename, s"*" => s".*")))
if isempty(file)
throw(ArgumentError("No matching filename was found for $(filenameIn)"))
elseif length(file) > 1
throw(ArgumentError("Ambiguous filename $(filenameIn)!"))
throw(ArgumentError("Ambiguous filename $(filename)!"))
end

filelist, fileID, pictsize = getfiletype(file[1], dir)
Expand Down
9 changes: 4 additions & 5 deletions src/vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,16 @@ function convertTECtoVTU(head, data, connectivity, filename="out")
end

"""
convertIDLtoVTK(filename; dir=".", gridType=1, verbose=false)
convertIDLtoVTK(filename; gridType=1, verbose=false)
Convert 3D BATSRUS *.out to VTK. If `gridType==1`, it converts to the rectilinear grid; if
`gridType==2`, it converts to the structured grid. If `filename` does not end with "out", it
tries to find the ".info" and ".tree" file with the same name tag and generates 3D
unstructured VTU file.
"""
function convertIDLtoVTK(filename::AbstractString; dir::String=".", gridType::Int=1,
verbose::Bool=false)
function convertIDLtoVTK(filename::AbstractString; gridType::Int=1, verbose::Bool=false)
if endswith(filename, ".out")
data = load(filename, dir=dir)
data = load(filename)

nVar = length(data.head.wnames)

Expand Down Expand Up @@ -202,7 +201,7 @@ function convertIDLtoVTK(filename::AbstractString; dir::String=".", gridType::In

else
# info, tree, and out files
data = load(filename*".out", dir=dir)
data = load(filename*".out")
batl = Batl(readhead(filename*".info"), readtree(filename)...)
connectivity = getConnectivity(batl)

Expand Down
Loading

0 comments on commit 2011af8

Please sign in to comment.