Skip to content

Commit fdd372a

Browse files
authored
Merge pull request #134 from JuliaGeodynamics/pa-cross
fix GeoData export
2 parents 23d9d4a + 4094505 commit fdd372a

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

src/utils.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ addfield(V::GeoData,new_fields::NamedTuple) = GeoData(V.lon.val, V.lat.val, V.de
7070
7171
Add `new_fields` fields to a `Q1Data` dataset; set `cellfield` to `true` if the field is a cell field; otherwise it is a vertex field
7272
"""
73-
function addfield(V::Q1Data,new_fields::NamedTuple; cellfield=false)
73+
function addfield(V::Q1Data,new_fields::NamedTuple; cellfield=false)
7474
if cellfield
7575
return Q1Data(V.x.val, V.y.val, V.z.val, V.fields, merge(V.cellfields, new_fields))
7676
else
@@ -122,7 +122,7 @@ end
122122

123123

124124

125-
"""
125+
"""
126126
cross_section_volume(Volume::AbstractGeneralGrid; dims=(100,100), Interpolate=false, Depth_level=nothing; Lat_level=nothing; Lon_level=nothing; Start=nothing, End=nothing, Depth_extent=nothing )
127127
128128
Creates a cross-section through a volumetric (3D) `GeoData` object.
@@ -259,7 +259,7 @@ cross_section_surface(Surface::GeoData; dims=(100,), Interpolate=false, Depth_le
259259
Creates a cross-section through a surface (2D) `GeoData` object.
260260
261261
- Cross-sections can be horizontal (map view at a given depth), if `Depth_level` is specified
262-
- They can also be vertical, either by specifying `Lon_level` or `Lat_level` (for a fixed lon/lat), or by defining both `Start=(lon,lat)` & `End=(lon,lat)` points.
262+
- They can also be vertical, either by specifying `Lon_level` or `Lat_level` (for a fixed lon/lat), or by defining both `Start=(lon,lat)` & `End=(lon,lat)` points. Start and End points will be in km!
263263
264264
- IMPORTANT: The surface to be extracted has to be given as a gridded GeoData object. It may also contain NaNs where it is not defined. Any points lying outside of the defined surface will be considered NaN.
265265
@@ -351,9 +351,14 @@ function cross_section_surface(S::AbstractGeneralGrid; dims=(100,), Interpolate=
351351

352352
end
353353

354-
# create GeoData structure with the interpolated points
355-
Data_profile = GeoData(Lon, Lat, depth_intp, (fields_new));
356-
354+
# create GeoData/CartData structure with the interpolated points
355+
if isa(S,GeoData)
356+
Data_profile = GeoData(Lon, Lat, depth_intp, fields_new);
357+
elseif isa(S,CartData)
358+
Data_profile = CartData(Lon, Lat, depth_intp, fields_new);
359+
else
360+
error("still to be implemented")
361+
end
357362
return Data_profile
358363
end
359364

@@ -545,9 +550,9 @@ Creates a cross-section through a `GeoData` object.
545550
julia> Lon,Lat,Depth = lonlatdepth_grid(10:20,30:40,(-300:25:0)km);
546551
julia> Data = Depth*2; # some data
547552
julia> Vx,Vy,Vz = ustrip(Data*3),ustrip(Data*4),ustrip(Data*5);
548-
julia> Data_set3D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)));
549-
julia> Data_cross = cross_section(Data_set3D, Depth_level=-100km)
550-
GeoData
553+
julia> Data_set3D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)));
554+
julia> Data_cross = cross_section(Data_set3D, Depth_level=-100km)
555+
GeoData
551556
size : (11, 11, 1)
552557
lon ϵ [ 10.0 : 20.0]
553558
lat ϵ [ 30.0 : 40.0]
@@ -601,7 +606,7 @@ CartData
601606
```
602607
"""
603608
function flatten_cross_section(V::CartData)
604-
609+
605610
x_new = sqrt.((V.x.val.-V.x.val[1,1,1]).^2 .+ (V.y.val.-V.y.val[1,1,1]).^2) # NOTE: the result is in km, as V.x and V.y are stored in km
606611

607612

@@ -1244,11 +1249,11 @@ function interpolate_datafields_2D(Original::GeoData, New::GeoData; Rotate=0.0,
12441249
return GeoData(New.lon.val,New.lat.val,Znew, fields_new)
12451250
end
12461251

1247-
"""
1252+
"""
12481253
Surf_interp = interpolate_datafields_2D(V::GeoData, x::AbstractRange, y::AbstractRange; Lat::Number, Lon::Number)
12491254
12501255
Interpolates a 3D data set `V` with a projection point `proj=(Lat, Lon)` on a plane defined by `x` and `y`, where `x` and `y` are uniformly spaced.
1251-
Returns the 2D array `Surf_interp`.
1256+
Returns the 2D array `Surf_interp`.
12521257
"""
12531258
function interpolate_datafields_2D(V::GeoData, x::AbstractRange, y::AbstractRange; Lat=49.9929, Lon=8.2473)
12541259
# Default: Lat=49.9929, Lon=8.2473 => Mainz (center of universe)
@@ -1340,7 +1345,7 @@ function InterpolateDataFields2D_vecs(EW_vec, NS_vec, depth, fields_new, EW, NS)
13401345

13411346
return depth_new, fields_new
13421347
end
1343-
1348+
13441349
# Extracts a sub-data set using indices
13451350
function ExtractDataSets(V::AbstractGeneralGrid, iLon, iLat, iDepth)
13461351

@@ -1815,4 +1820,4 @@ function inpoly_fast(PolyX::Vector{T}, PolyY::Vector{T}, x::T, y::T) where T <:
18151820
end
18161821
end
18171822
return inside
1818-
end
1823+
end

test/test_utils.jl

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ using GeophysicalModelGenerator
77
Lon,Lat,Depth = lonlatdepth_grid(10:20,30:40,-50km);
88
Data1 = Depth*2; # some data
99
Vx1,Vy1,Vz1 = Data1*3,Data1*4,Data1*5
10-
Data_set2D = GeoData(Lon,Lat,Depth,(Depthdata=Data1,LonData1=Lon, Velocity=(Vx1,Vy1,Vz1)))
11-
Data_set2D0 = GeoData(Lon,Lat,Depth,(Depthdata=Data1,LonData1=Lon))
10+
Data_set2D = GeoData(Lon,Lat,Depth,(Depthdata=Data1,LonData1=Lon, Velocity=(Vx1,Vy1,Vz1)))
11+
Data_set2D0 = GeoData(Lon,Lat,Depth,(Depthdata=Data1,LonData1=Lon))
1212
@test_throws ErrorException cross_section(Data_set2D, Depth_level=-10)
1313

1414
# Test interpolation of depth to a given cartesian XY-plane
@@ -22,7 +22,7 @@ plane2 = interpolate_datafields_2D(Data_set2D, proj, x, y)
2222
Lon1,Lat1,Depth1 = lonlatdepth_grid(12:18,33:39,-50km);
2323
Data2 = Depth1*2; # some data
2424
Vx1,Vy1,Vz1 = Data2*3,Data2*4,Data2*5
25-
Data_set2D_1 = GeoData(Lon1,Lat1,Depth1,(Depthdata1=Data2,LonData2=Lon1))
25+
Data_set2D_1 = GeoData(Lon1,Lat1,Depth1,(Depthdata1=Data2,LonData2=Lon1))
2626

2727
plane3 = interpolate_datafields_2D(Data_set2D0, Data_set2D_1)
2828
@test sum(plane3.fields.Depthdata) -4900.0km
@@ -35,23 +35,23 @@ plane3 = interpolate_datafields_2D(Data_set2D0, Data_set2D_1)
3535
Lon,Lat,Depth = lonlatdepth_grid(10:20,30:40,(-300:25:0)km);
3636
Data = Depth*2; # some data
3737
Vx,Vy,Vz = ustrip(Data*3)*km/s,ustrip(Data*4)*km/s,ustrip(Data*5)*km/s;
38-
Data_set3D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
38+
Data_set3D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
3939

4040
# Test addfield
41-
Data_set3D = addfield(Data_set3D,"Lat", Lat)
41+
Data_set3D = addfield(Data_set3D,"Lat", Lat)
4242
@test keys(Data_set3D.fields) == (:Depthdata, :LonData, :Velocity, :Lat)
4343

44-
Data_set3D = addfield(Data_set3D,(;Lat, Lon))
44+
Data_set3D = addfield(Data_set3D,(;Lat, Lon))
4545
@test keys(Data_set3D.fields) == (:Depthdata, :LonData, :Velocity, :Lat, :Lon)
4646

4747
# Create 3D cartesian dataset
48-
Data_setCart3D = CartData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
48+
Data_setCart3D = CartData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
4949

5050
# Create 3D volume with some fake data
5151
Lon,Lat,Depth = lonlatdepth_grid(10:20,30:40,(0:-25:-300)km);
5252
Data = Depth*2; # some data
5353
Vx,Vy,Vz = ustrip(Data*3)*km/s,ustrip(Data*4)*km/s,ustrip(Data*5)*km/s;
54-
Data_set3D_reverse = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
54+
Data_set3D_reverse = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon, Velocity=(Vx,Vy,Vz)))
5555

5656
# Create cross-sections in various directions (no interpolation which is default)
5757
test_cross = cross_section(Data_set3D, Depth_level=-100km)
@@ -99,6 +99,20 @@ test_cross = cross_section(Data_set3D, Start=(10,30), End=(20,40), dims=(
9999
#@test size(test_cross_rev.fields[3][2])==(50,100,1)
100100
#@test write_paraview(test_cross_rev, "profile_test_rev")[1]=="profile_test_rev.vts"
101101

102+
# Cross section of a topography
103+
depth_values = [rand(0:0.1:3.5)]
104+
Lon, Lat, Depth =lonlatdepth_grid(10:20, 30:40, depth_values[:]);
105+
Data_Topo = GeoData(Lon, Lat, Depth, (Depthdata=Depth,))
106+
Data_Topo_geo= cross_section(Data_Topo, Start=(10,30), End=(20,40), dims=(50,100), Interpolate=true)
107+
@test Data_Topo_geo isa GeoData
108+
109+
Lon,Lat,Depth = lonlatdepth_grid(5:25,20:50,0);
110+
Depth = cos.(Lon/5).*sin.(Lat)*10;
111+
Data_surf = GeoData(Lon,Lat,Depth,(Z=Depth,));
112+
Data_surf_cart = convert2CartData(Data_surf, proj);
113+
Data_surf_cross = cross_section(Data_surf_cart, Start=(-1693,2500), End=(-1000,3650), dims=(50,100), Interpolate=true)
114+
@test Data_surf_cross isa CartData
115+
102116
# Cross-section with cartesian data
103117
test_cross = cross_section(Data_setCart3D, Lon_level=15, dims=(50,100), Interpolate=true)
104118
@test size(test_cross.fields[3][2])==(1,50,100)
@@ -173,16 +187,16 @@ Data_pert = subtract_horizontalmean(Data, Percentage=true) # 3D w
173187
@test Data_pert[1000] == 0.0
174188

175189
Data2D = Data[:,1,:];
176-
Data_pert = subtract_horizontalmean(Data2D, Percentage=true) # 2D version with units [dp the same along a vertical profile]
190+
Data_pert = subtract_horizontalmean(Data2D, Percentage=true) # 2D version with units [dp the same along a vertical profile]
177191

178-
Data_set2D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon,Pertdata=Data_pert ,Velocity=(Vx,Vy,Vz)))
192+
Data_set2D = GeoData(Lon,Lat,Depth,(Depthdata=Data,LonData=Lon,Pertdata=Data_pert ,Velocity=(Vx,Vy,Vz)))
179193
@test Data_set2D.fields[3][10,8,1] == 0
180194

181195

182196
# Create surface ("Moho")
183197
Lon,Lat,Depth = lonlatdepth_grid(10:20,30:40,-40km);
184198
Depth = Depth + Lon*km; # some fake topography on Moho
185-
Data_Moho = GeoData(Lon,Lat,Depth,(MohoDepth=Depth,LonData=Lon,TestData=(Depth,Depth,Depth)))
199+
Data_Moho = GeoData(Lon,Lat,Depth,(MohoDepth=Depth,LonData=Lon,TestData=(Depth,Depth,Depth)))
186200

187201

188202
# Test intersecting a surface with 2D or 3D data sets
@@ -218,7 +232,7 @@ Data_VoteMap = votemap(Data_set3D_reverse, "Depthdata<-560",dims=(10,10,10))
218232
@test Data_VoteMap.fields[:votemap][101]==0
219233
@test Data_VoteMap.fields[:votemap][100]==1
220234

221-
# Combine 2 datasets
235+
# Combine 2 datasets
222236
Data_VoteMap = votemap([Data_set3D_reverse, Data_set3D], ["Depthdata<-560","LonData>19"],dims=(10,10,10))
223237
@test Data_VoteMap.fields[:votemap][10,9,1]==2
224238
@test Data_VoteMap.fields[:votemap][9 ,9,1]==1
@@ -253,9 +267,9 @@ cross_tmp = cross_section(Data_EQ,Lat_level=36.2,section_width=10km)
253267
@test cross_tmp.fields.lat_proj[10]==36.2 # check if the projected latitude level is the chosen one
254268

255269
cross_tmp = cross_section(Data_EQ,Lon_level=16.4,section_width=10km)
256-
@test cross_tmp.fields.lon_proj[10]==16.4 # check if the projected longitude level is the chosen one
270+
@test cross_tmp.fields.lon_proj[10]==16.4 # check if the projected longitude level is the chosen one
257271
cross_tmp = cross_section(Data_EQ,Start=(15.0,35.0),End=(17.0,37.0),section_width=10km)
258-
@test cross_tmp.fields.lon_proj[20] ==15.314329874961091
272+
@test cross_tmp.fields.lon_proj[20] ==15.314329874961091
259273
@test cross_tmp.fields.lat_proj[20] == 35.323420618580585
260274

261275
# test inPolygon

0 commit comments

Comments
 (0)