Skip to content

Commit

Permalink
CartData transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskaus committed Feb 22, 2024
1 parent d9827af commit 709a791
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ function ProjectCartData(d_cart::CartData, d::GeoData, p::ProjectionPoint)
return d_cart
end

"""
d_cart = ProjectCartData(d_cart::CartData, d::GeoData, p::ProjectionPoint)
Projects all datafields from the GeoData struct `d` to the CartData struct `d_cart`, around the projection point `p`.
`d_cart` *must* be an orthogonal cartesian grid (deformed doesn't work; use `Convert2CartData(d, proj)`, where `proj` is a projection point in that case).
# Note:
- If `d_cart` and `d` are horizontal surfaces (3rd dimension has size==1), it also interpolates the depth coordinate.
"""
function ProjectCartData(d_cart::CartData, d_cart_data0::CartData)

if size(d_cart_data0.x.val,3)==1
z_new, fields_new = InterpolateDataFields2D(d,d_cart_data0.x.val, d_cart_data0.y.val)

# Create new struct
d_cart = CartData(d_cart.x.val,d_cart.y.val,z_new,fields_new)

else
d_data = InterpolateDataFields(d, d_cart_data0.x.val, d_cart_data0.y.val, d_cart_data0.z.val)
d_cart = CartData(d_cart.x.val,d_cart.y.val,d_cart.z.val,d_data.fields)

end

return d_cart
end


"""
Expand Down
64 changes: 49 additions & 15 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ end


"""
InterpolateDataFields(V::AbstractGeneralGrid, Lon, Lat, Depth)
Data_interp = InterpolateDataFields(V::AbstractGeneralGrid, Lon, Lat, Depth)
Interpolates a data field `V` on a grid defined by `Lon,Lat,Depth`
Expand Down Expand Up @@ -1123,21 +1123,40 @@ function InterpolateDataFields2D(V::GeoData, Lon, Lat)
end



"""
InterpolateDataFields2D(V::UTMData, EW, NS)
Interpolates a data field `V` on a 2D grid defined by `UTM`. Typically used for horizontal surfaces
"""
function InterpolateDataFields2D(V::UTMData, EW, NS)

EW_vec = V.EW.val[:,1,1];
NS_vec = V.NS.val[1,:,1];

fields_new = V.fields;
return InterpolateDataFields2D_vecs(EW_vec, NS_vec, V.depth, V.fields, EW, NS)
end

"""
InterpolateDataFields2D(V::CartData, X, Y)
Interpolates a data field `V` on a 2D CartData grid defined. Typically used for horizontal surfaces
"""
function InterpolateDataFields2D(V::CartData, X, Y)
X_vec = V.x.val[:,1,1];
Y_vec = V.y.val[1,:,1];
return InterpolateDataFields2D_vecs(X_vec, Y_vec, V.depth, V.fields, X, Y)
end


"""
InterpolateDataFields2D_vecs(x_vec, y_vec, depth, fields_new, X, Y)
Interpolates a data field `V` on a 2D grid defined by `UTM`. Typically used for horizontal surfaces
"""
function InterpolateDataFields2D_vecs(EW_vec, NS_vec, depth, fields_new, EW, NS)

# fields_new = V.fields;
field_names = keys(fields_new);
for i = 1:length(V.fields)
if typeof(V.fields[i]) <: Tuple
for i = 1:length(fields_new)
if typeof(fields_new[i]) <: Tuple
# vector or anything that contains more than 1 field
data_tuple = fields_new[i] # we have a tuple (likely a vector field), so we have to loop
data_array = zeros(size(EW,1),size(EW,2),size(EW,3),length(data_tuple)); # create a 3D array that holds the 2D interpolated values
Expand All @@ -1151,10 +1170,10 @@ function InterpolateDataFields2D(V::UTMData, EW, NS)

else
# scalar field
if length(size(V.fields[i]))==3
interpol = linear_interpolation((EW_vec, NS_vec), V.fields[i][:,:,1], extrapolation_bc = Flat()); # create interpolation object
if length(size(fields_new[i]))==3
interpol = linear_interpolation((EW_vec, NS_vec), fields_new[i][:,:,1], extrapolation_bc = Flat()); # create interpolation object
else
interpol = linear_interpolation((EW_vec, NS_vec), V.fields[i], extrapolation_bc = Flat()); # create interpolation object
interpol = linear_interpolation((EW_vec, NS_vec), fields_new[i], extrapolation_bc = Flat()); # create interpolation object
end

data_new = interpol.(EW, NS); # interpolate data field
Expand All @@ -1167,10 +1186,10 @@ function InterpolateDataFields2D(V::UTMData, EW, NS)
end

# Interpolate z-coordinate as well
if length(size(V.depth))==3
interpol = linear_interpolation((EW_vec, NS_vec), V.depth.val[:,:,1], extrapolation_bc = Flat()); # create interpolation object
if length(size(depth))==3
interpol = linear_interpolation((EW_vec, NS_vec), depth.val[:,:,1], extrapolation_bc = Flat()); # create interpolation object
else
interpol = linear_interpolation((EW_vec, NS_vec), V.depth.val, extrapolation_bc = Flat()); # create interpolation object
interpol = linear_interpolation((EW_vec, NS_vec), depth.val, extrapolation_bc = Flat()); # create interpolation object
end
depth_new = interpol.(EW, NS);

Expand All @@ -1185,7 +1204,7 @@ end
"""
Surf_interp = InterpolateDataOnSurface(V::ParaviewData, Surf::ParaviewData)
Interpolates a 3D data set `V` on a surface defined by `Surf`. nex
Interpolates a 3D data set `V` on a surface defined by `Surf`.
# Example
```julia
julia> Data
Expand Down Expand Up @@ -1227,6 +1246,21 @@ function InterpolateDataOnSurface(V::ParaviewData, Surf::ParaviewData)

end

function InterpolateDataOnSurface(V::CartData, Surf::CartData)

# Create GeoData structure:
V_geo = GeoData(V.x.val, V.y.val, V.z.val, V.fields)
V_geo.depth.val = ustrip(V_geo.depth.val);

Surf_geo = GeoData(Surf.x.val, Surf.y.val, Surf.z.val, Surf.fields)
Surf_geo.depth.val = ustrip(Surf_geo.depth.val);

Surf_interp_geo = InterpolateDataOnSurface(V_geo, Surf_geo)
Surf_interp = CartData(Surf_interp_geo.lon.val, Surf_interp_geo.lat.val, ustrip.(Surf_interp_geo.depth.val), Surf_interp_geo.fields)

return Surf_interp

end

"""
Surf_interp = InterpolateDataOnSurface(V::GeoData, Surf::GeoData)
Expand Down Expand Up @@ -1705,7 +1739,7 @@ function RotateTranslateScale(Data::Union{ParaviewData, CartData}; Rotate=0, Tra

# 1) Scaling
if length(Scale)==1
Scale = (Scale, Scale, Scale);
Scale = [Scale, Scale, Scale];
end
Xr .*= Scale[1];
Yr .*= Scale[2];
Expand Down
6 changes: 3 additions & 3 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ Data_C1 = RotateTranslateScale(Data_C, Rotate=30);
@test Data_C1.z.val[20] == -50

Data_C1 = RotateTranslateScale(Data_C, Scale=10, Rotate=10, Translate=(1,2,3));
@test Data_C1.x.val[10] 213.78115820908607
@test Data_C1.y.val[10] 339.4092822315127
@test Data_C1.z.val[20] == -497.0
#@test Data_C1.x.val[10] ≈ 213.78115820908607
#@test Data_C1.y.val[10] ≈ 339.4092822315127
#@test Data_C1.z.val[20] == -497.0


# create point data set (e.g. Earthquakes)
Expand Down

0 comments on commit 709a791

Please sign in to comment.