# GeoData: "array has wrong ordering"

**URL:** <https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916>\
**Category:** General Usage\
**Tags:** question, geodatascience\
**Created:** [February 8, 2024, 9:30am UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916 "2024-02-08T09:30:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aqeel\_Abbas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aqeel_abbas/32/206593_2.png) [@Aqeel\_Abbas](https://discourse.julialang.org/u/Aqeel_Abbas)\
**Post date:** [February 8, 2024, 9:30am UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/1 "2024-02-08T09:30:38Z")

</div>

Dear All,

I am a new user of Julia following this Example. I got an error using the below code, any suggestions or comments will be appreciated :

> using GeophysicalModelGenerator, GeoDatasets,DelimitedFiles
> 
> # Load data:
> 
> data=readdlm(“peakdelay\_3\_Degrees\_Hz.txt”,‘,’,‘\n’, skipstart=0,header=false)  
> lon = data[:,1];  
> lat = data[:,2];  
> depth = 1\*data[:,3];  
> peakdelay = data[:,4];
> 
> resolution = (length(unique(lon)), length(unique(lat)), length(unique(depth)))
> 
> Lon = reshape(lon, resolution);  
> Lat = reshape(lat, resolution);  
> Depth = reshape(depth, resolution);  
> peakdelay = reshape(peakdelay, resolution);
> 
> EQ\_Data = GeoData(Lon,Lat,Depth,(Peakdelay=peakdelay,));

I am getting this error while running the last command line, I have tried to double check each variable :

> ERROR: It appears that the lon array has a wrong ordering  
> Stacktrace:  
> [1] error(s::String)  
> @ Base ./error.jl:35  
> [2] GeoData(lon::Array{Float64, 3}, lat::Array{Float64, 3}, depth::Array{Float64, 3}, fields::@NamedTuple{Peakdelay::Array{Float64, 3}}, atts::Nothing)  
> @ GeophysicalModelGenerator ~/.julia/packages/GeophysicalModelGenerator/AiGOf/src/data\_types.jl:175  
> [3] GeoData(lon::Array{Float64, 3}, lat::Array{Float64, 3}, depth::Array{Float64, 3}, fields::@NamedTuple{Peakdelay::Array{Float64, 3}})  
> @ GeophysicalModelGenerator ~/.julia/packages/GeophysicalModelGenerator/AiGOf/src/data\_types.jl:158  
> [4] top-level scope  
> @ REPL[19]:1

The file input is in this format :

> 104.347,29.347,1500,0,0  
> 104.347,29.347,433.333333333333,0,0  
> 104.347,29.347,-633.333333333333,0,0  
> 104.347,29.347,-1700,0,0  
> 104.347,29.347,-2766.66666666667,0,0  
> 104.347,29.347,-3833.33333333333,0,0  
> 104.347,29.347,-4900,0,0  
> 104.347,29.347,-5966.66666666667,0,0  
> 104.347,29.347,-7033.33333333333,0,0  
> 104.347,29.347,-8100,0,0  
> 104.347,29.3705384615385,1500,0,0  
> 104.347,29.3705384615385,433.333333333333,0,0  
> 104.347,29.3705384615385,-633.333333333333,0,0  
> 104.347,29.3705384615385,-1700,0,0  
> 104.347,29.3705384615385,-2766.66666666667,0,0  
> 104.347,29.3705384615385,-3833.33333333333,0,0  
> 104.347,29.3705384615385,-4900,0,0

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [February 8, 2024, 11:59am UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/2 "2024-02-08T11:59:19Z")

</div>

(I don’t personally use these packages, just trying to help.) That error comes from here in the source, specifically from line 175.

> <https://github.com/JuliaGeodynamics/GeophysicalModelGenerator.jl/blob/ad010080eced2f8a7215b11c0c4b3b9095da3445/src/data_types.jl#L172-L180>

I’m trying to mentally parse that `if` condition, and it reads to me like it expects an array with a particular orientation, e.g. row-major vs column-major ordering, and will throw an error if the provided array is the opposite orientation.

---

<div class="post-metadata">

**Author:** ![boriskaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boriskaus/32/34591_2.png) [@boriskaus](https://discourse.julialang.org/u/boriskaus)\
**Post date:** [February 8, 2024, 1:02pm UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/3 "2024-02-08T13:02:12Z")

</div>

Can you perhaps upload the “peakdelay\_3\_Degrees\_Hz.txt” file, such that I can reproduce it?  
In this example, `Lon`, `Lat`,`Depth` should be 3D arrays with `Lon` changing in the first dimension as in:

```Julia
julia> using GeophysicalModelGenerator
julia> Lon,Lat,Depth=XYZGrid(0:2,1:4,5:6);
julia> Lon
3×4×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 0.0 0.0
 1.0 1.0 1.0 1.0
 2.0 2.0 2.0 2.0

[:, :, 2] =
 0.0 0.0 0.0 0.0
 1.0 1.0 1.0 1.0
 2.0 2.0 2.0 2.0

```

I suspect that your `Lon` changes in the second dimension, which would throw the error you see.

---

<div class="post-metadata">

**Author:** ![Aqeel\_Abbas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aqeel_abbas/32/206593_2.png) [@Aqeel\_Abbas](https://discourse.julialang.org/u/Aqeel_Abbas)\
**Post date:** [February 8, 2024, 1:13pm UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/4 "2024-02-08T13:13:32Z")

</div>

Thank you for your kind response. I have send you a file in person with some details.

---

<div class="post-metadata">

**Author:** ![boriskaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boriskaus/32/34591_2.png) [@boriskaus](https://discourse.julialang.org/u/boriskaus)\
**Post date:** [February 8, 2024, 1:18pm UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/5 "2024-02-08T13:18:59Z")

</div>

ok, for general info, here some worked-out examples:  
_Correct_

```Julia
julia> Lon,Lat,Depth=XYZGrid(0:2,1:4,5:6);
julia> Lon
3×4×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 0.0 0.0
 1.0 1.0 1.0 1.0
 2.0 2.0 2.0 2.0

[:, :, 2] =
 0.0 0.0 0.0 0.0
 1.0 1.0 1.0 1.0
 2.0 2.0 2.0 2.0
julia> GeoData(Lon,Lat,Depth,(;Depth))
GeoData 
  size : (3, 4, 2)
  lon ϵ [0.0 : 2.0]
  lat ϵ [1.0 : 4.0]
  depth ϵ [5.0 : 6.0]
  fields : (:Depth,)
  attributes: ["note"]

```

_Wrong:_

```julia
julia> Lat1,Lon1,Depth1=XYZGrid(0:2,1:4,5:6);
julia> Lon1
3×4×2 Array{Float64, 3}:
[:, :, 1] =
 1.0 2.0 3.0 4.0
 1.0 2.0 3.0 4.0
 1.0 2.0 3.0 4.0

[:, :, 2] =
 1.0 2.0 3.0 4.0
 1.0 2.0 3.0 4.0
 1.0 2.0 3.0 4.0
julia> GeoData(Lon1,Lat1,Depth1,(;Depth))
ERROR: It appears that the lon array has a wrong ordering
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] GeoData(lon::Array{Float64, 3}, lat::Array{Float64, 3}, depth::Array{Float64, 3}, fields::@NamedTuple{Depth::Array{Float64, 3}}, atts::Nothing)
   @ GeophysicalModelGenerator ~/.julia/packages/GeophysicalModelGenerator/AiGOf/src/data_types.jl:175
 [3] GeoData(lon::Array{Float64, 3}, lat::Array{Float64, 3}, depth::Array{Float64, 3}, fields::@NamedTuple{Depth::Array{Float64, 3}})
   @ GeophysicalModelGenerator ~/.julia/packages/GeophysicalModelGenerator/AiGOf/src/data_types.jl:158
 [4] top-level scope
   @ REPL[88]:1

```

_Fix:_

```Julia
julia> Lon2 = permutedims(Lon1,(2,1,3));
julia> Lat2 = permutedims(Lat1,(2,1,3));
julia> Depth2 = permutedims(Depth1,(2,1,3));

julia> GeoData(Lon2,Lat2,Depth2,(;Depth2))
GeoData 
  size : (4, 3, 2)
  lon ϵ [1.0 : 4.0]
  lat ϵ [0.0 : 2.0]
  depth ϵ [5.0 : 6.0]
  fields : (:Depth2,)
  attributes: ["note"]

```

---

<div class="post-metadata">

**Author:** ![boriskaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boriskaus/32/34591_2.png) [@boriskaus](https://discourse.julialang.org/u/boriskaus)\
**Post date:** [February 8, 2024, 1:44pm UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/6 "2024-02-08T13:44:21Z")

</div>

ok, in your specific example the order is a bit different (depth changes first), so one way to do this is:

```Julia
using GeophysicalModelGenerator, DelimitedFiles

data=readdlm("1_test_peakdelay.txt",',','\n', skipstart=0,header=false);

lon = data[:,1];
lat = data[:,2];
depth = data[:,3];
peakdelay = data[:,4];
res = (length(unique(lon)), length(unique(lat)), length(unique(depth)))

Lon = zeros(res);
Lat = zeros(res);
Z = zeros(res);
Peakdelay = zeros(res);

num = 1
for i in 1:res[2], j in 1:res[1], k in 1:res[3]
    Lon[i,j,k] = lon[num];
    Lat[i,j,k] = lat[num];
    Z[i,j,k] = depth[num];
    Peakdelay[i,j,k] = peakdelay[num];
    num += 1
end 

EQ_Data = GeoData(Lon,Lat,Z,(; Peakdelay,));

```

---

<div class="post-metadata">

**Author:** ![boriskaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boriskaus/32/34591_2.png) [@boriskaus](https://discourse.julialang.org/u/boriskaus)\
**Post date:** [February 8, 2024, 1:56pm UTC](https://discourse.julialang.org/t/geodata-array-has-wrong-ordering/109916/7 "2024-02-08T13:56:48Z")

</div>

or slightly shorter:

```Julia
using GeophysicalModelGenerator, DelimitedFiles

data=readdlm("1_test_peakdelay.txt",',','\n', skipstart=0,header=false);

lon,lat,depth = data[:,1], data[:,2], data[:,3];
peakdelay = data[:,4];

res = (length(unique(lon)), length(unique(lat)), length(unique(depth)))
Lon = zeros(res);
Lat = zeros(res);
Z = zeros(res);
Peakdelay = zeros(res);

num = 1
for i in 1:res[2], j in 1:res[1], k in 1:res[3]
    I = CartesianIndex(i,j,k)
    Lon[I], Lat[I], Z[I] = lon[num], lat[num], depth[num];
    Peakdelay[I] = peakdelay[num];
    num += 1
end 

EQ_Data = GeoData(Lon,Lat,Z,(; Peakdelay,));

```
